-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserverAPI.py
64 lines (61 loc) · 2.46 KB
/
webserverAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import subprocess
import re
class Webserver:
"""
Module `Webserver`
Shows all running webservers on the system
`self.webDic={}` Dictionary which stores all process of webservers which runs on 80 port
`self.web1Dic={}` Dictionary which stores all process of webservers which runs on 443 port
`self.webList=[]` List that contains cluster of dictionaries
"""
def __init__(self):
self.webDic={}
self.web1Dic={}
self.webList=[]
def display_webservers(self):
"""
Displays webservers runs on port 80 and 443
"""
webs=os.popen("sudo netstat -ntlp | grep 80").read().strip().splitlines()
webs1=os.popen("sudo netstat -ntlp | grep 443").read().strip().splitlines()
for web in webs:
split=re.split('[\s]+',web)
self.webDic["protcol"]=split[0]
self.webDic["receive q"]=split[1]
self.webDic["send q"]=split[2]
self.webDic["local address"]=split[3]
self.webDic["foreign address"]=split[4]
self.webDic["state"]=split[5]
split_ID=split[6].split("/")
self.webDic["pid"]=split_ID[0]
self.webDic["programme name"]=split_ID[1]
self.webList.append(self.webDic.copy())
for web in webs1:
split=re.split('[\s]+',web)
self.web1Dic["protcol"]=split[0]
self.web1Dic["receive q"]=split[1]
self.web1Dic["send q"]=split[2]
self.web1Dic["local address"]=split[3]
self.web1Dic["foreign address"]=split[4]
self.web1Dic["state"]=split[5]
split_ID=split[6].split("/")
self.web1Dic["pid"]=split_ID[0]
self.web1Dic["programme name"]=split_ID[1]
self.webList.append(self.web1Dic.copy())
return self.webList
def handle_webserver(self,name,command):
"""
Starts,stops and restarts given servers
`name` name of the server
`command` operation command
"""
if command=="start":
os.system("sudo systemctl start {}".format(name))
return {"server":name,"status":"started"}
elif command=="stop":
os.system("sudo systemctl stop {}".format(name))
return {"server":name,"status":"stopped"}
elif command=="restart":
os.system("sudo systemctl restart {}".format(name))
return {"server":name,"status":"restarted"}