-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
175 lines (163 loc) · 6.17 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
''' Server script, enabling an http server in a directory '''
import http.server
import socketserver
import sys
import threading
import os
import ConColors
import NetworkConfig
PARAMETERS = {"HOST": NetworkConfig.ipaddress(), "PORT": "4000",
"DIR": os.getcwd(),
"SERVER_THREAD": None, "HANDLER": None,
"HTTPD": socketserver.TCPServer}
class Handler(http.server.SimpleHTTPRequestHandler):
''' Wrapper for http server handler '''
def log_message(self, _format, *args):
return
class StatusThread(threading.Thread):
''' Thread wrapper class that includes kill functionality '''
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
super(StatusThread, self).__init__()
self.statusevent = threading.Event()
def run(self):
if self._target:
self._target(*self._args, **self._kwargs)
def stop(self):
''' kill thread '''
self.statusevent.set()
def stopped(self):
''' Query thread status '''
return self.statusevent.is_set()
def parse_args(_argin):
''' parse_args '''
if _argin.startswith("--"):
try:
function_handle = getattr(sys.modules[__name__], _argin[2:])
return function_handle
except AttributeError:
print(ConColors.RED + "Unknown Command: " + ConColors.BLACK + _argin[2:])
return "ERR"
else:
return "params"
def parse_line(_arguments):
''' parse_line '''
function = main
parameters = []
for i in range(0, len(_arguments)):
_obj = parse_args(_arguments[i])
if _obj != "params" and _obj != "ERR" and _arguments[i] != "--parse_line":
execute(function, parameters)
function = _obj
parameters = []
else:
parameters.append(_arguments[i])
execute(function, parameters)
def execute(function, params):
''' Execute function with the parameters list '''
handled = False
try:
if function != main:
if params:
function(params)
else:
try:
function()
except TypeError:
handled = True
function([])
except TypeError:
if not handled:
print(ConColors.RED + "Error in function " + function.__name__ +\
": Insufficient parameters" + ConColors.BLACK)
def main():
''' Program Entry Point '''
send = []
if len(sys.argv) >= 1:
for i in range(1, len(sys.argv)):
send.append(sys.argv[i])
parse_line(send)
show()
while True:
result = input(ConColors.YELLOW + "Server: " + ConColors.BLACK)
if result:
result = "--" + result
params = result.split()
error = False
for i in range(1, len(params)):
if i % 2 == 1:
if params[i].startswith("--"):
params[i] = params[i][2:]
else:
error = True
print(ConColors.RED + "Unknown parameter: " + ConColors.BLACK + params[i])
else:
if params[i].startswith("--"):
error = True
print(ConColors.RED + "Unknown parameter: " + ConColors.BLACK + params[i])
if not error:
parse_line(params)
def show():
''' Print out current status of the server '''
print()
print("Current Parameters: ")
print(" Host: " + PARAMETERS["HOST"])
print(" Port: " + PARAMETERS["PORT"])
print(" Dir: " + PARAMETERS["DIR"])
print("Device Parameters: ")
print(" IP : " + NetworkConfig.ipaddress())
print(" Name: " + NetworkConfig.hostname())
print()
def set(params):
''' Set dictionary values '''
for i in range(0, int(len(params) / 2)):
j = i * 2
if j + 1 < len(params):
PARAMETERS[params[j].upper()] = params[j + 1]
print("Setting " + ConColors.GREEN + params[j].upper() + ConColors.BLACK +\
" to " + params[j + 1])
def server(params):
''' Server method handler '''
for param in params:
if param.upper() == "START":
print("")
print(ConColors.GREEN + "Starting Server" + ConColors.BLACK)
print(" Server: " + ConColors.BLUE + "Initializing Thread" + ConColors.BLACK)
os.chdir(PARAMETERS["DIR"])
PARAMETERS["SERVER_THREAD"] = StatusThread()
PARAMETERS["SERVER_THREAD"]._target = ___start_server
PARAMETERS["SERVER_THREAD"]._args = (PARAMETERS["HOST"],
PARAMETERS["PORT"])
PARAMETERS["SERVER_THREAD"].daemon = True
print(" Server: " + ConColors.BLUE + "Starting Thread" + ConColors.BLACK)
PARAMETERS["SERVER_THREAD"].start()
print(" Server: " + ConColors.GREEN + "Ready" + ConColors.BLACK)
print("")
elif param.upper() == "STOP":
print("")
print(ConColors.RED + "Stopping Server" + ConColors.BLACK)
print(" Server: " + ConColors.BLUE + "Shutting down" + ConColors.BLACK)
PARAMETERS["HTTPD"].shutdown()
print(" Server: " + ConColors.BLUE + "Ending socket" + ConColors.BLACK)
PARAMETERS["HTTPD"].socket.close()
print(" Server: " + ConColors.BLUE + "Ending Thread" + ConColors.BLACK)
PARAMETERS["SERVER_THREAD"].stop()
PARAMETERS["SERVER_THREAD"].join()
print(" Server: " + ConColors.RED + "Stopped" + ConColors.BLACK)
print("")
def ___start_server(_host, _port):
''' Server Start Thread '''
try:
PARAMETERS["HANDLER"] = Handler
with socketserver.TCPServer((_host, int(_port)), PARAMETERS["HANDLER"]) as httpd:
PARAMETERS["HTTPD"] = httpd
httpd.serve_forever()
except PermissionError:
pass
def exit():
''' exit command forwarder '''
quit()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(ConColors.BLUE + "Interruption Detected" + ConColors.BLACK)