-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.min.py
More file actions
46 lines (40 loc) · 1.25 KB
/
server.min.py
File metadata and controls
46 lines (40 loc) · 1.25 KB
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
'''import http.server'''
import http.server
import socketserver
import sys
import threading
import os
import urllib
class Handler(http.server.SimpleHTTPRequestHandler):
''' Wrapper for http server handler '''
def log_message(self, _format, *args):
return
def do_GET(self):
urlparts = urllib.parse.urlparse(self.path)
request_file_path = urlparts.path.strip('/')
if not os.path.exists(request_file_path):
self.path = '/'
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def main():
''' Program Entry Point '''
_host = sys.argv[1]
_port = int(sys.argv[2])
server_thread = threading.Thread(target=start_server, args=(_host, _port))
server_thread.daemon = True
server_thread.start()
while True:
result = input("")
if result:
if "exit" in result:
sys.exit(1)
def start_server(_host, _port):
''' Server Thread '''
handler = Handler
with socketserver.TCPServer((_host, _port), handler) as httpd:
print("Starting server:")
print(" host: " + sys.argv[1])
print(" port: " + sys.argv[2])
httpd.error_message_format = '';
httpd.serve_forever()
if __name__ == "__main__":
main()