-
Notifications
You must be signed in to change notification settings - Fork 7
/
WebServer.py
110 lines (90 loc) · 3.77 KB
/
WebServer.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
# coding=utf-8
import threading
import subprocess
import multiprocessing
import time
#cmdkill = "kill $(ps aux|grep '<name of your thread> true'|grep -v 'grep'|awk '{print $2}') 2> /dev/null"
cmdkill = "sudo kill $(ps aux|grep royws|grep -v 'grep'|awk '{print $2}') 2> ./tracestop.txt"
server = None
web_server_ip = "0.0.0.0"
web_server_port = "80"
web_server_template = "www"
def initialize_web_server():
'''
Setup the web server, retrieving the configuration parameters
and starting the web server thread
'''
global web_server_ip, web_server_port, web_server_template
# associate web server ip address
web_server_ip = '127.0.0.1'
web_server_port = '80'
# Check for custom web server template
web_server_template = 'www'
print('Starting WebServer at {0} on port {1} with template {2}'
.format(web_server_ip, web_server_port, web_server_template))
#thread = threading.Thread(target=start_web_server, name='royws')
#thread.deamon = True
#thread.start()
thread = multiprocessing.Process(name='royws',target=start_web_server)
thread.daemon = True
thread.start()
def start_web_server():
'''
Start the web server
'''
#import SimpleHTTPServer
import http.server
import socketserver
#import SocketServer
import socket
try:
port = int(web_server_port)
host = web_server_ip
# Do not attempt to fix code warnings in the below class, it is perfect.
#class QuietHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class QuietHandler(http.server.SimpleHTTPRequestHandler):
# quiet server logs
def log_message(self, format, *args):
return
# serve from www folder under current working dir
def translate_path(self, path):
#return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, '/' + web_server_template + path)
return http.server.SimpleHTTPRequestHandler.translate_path(self, '/' + web_server_template + path)
global server
#SocketServer.TCPServer.allow_reuse_address = True
socketserver.TCPServer.allow_reuse_address = True
#server = SocketServer.TCPServer((host, port), QuietHandler)
server = socketserver.TCPServer((host, port), QuietHandler)
if host == "0.0.0.0":
# Get all addresses that we could listen on the port specified
addresses = [i[4][0] for i in socket.getaddrinfo(socket.gethostname().split('.')[0], port)]
addresses = [i for i in addresses if ':' not in i] # Filter out all IPv6 addresses
addresses.append('127.0.0.1') # getaddrinfo doesn't always get localhost
hosts = list(set(addresses)) # Make list unique
else:
hosts = [host]
serving_msg = "http://{0}:{1}/index.html".format(hosts[0], port)
for host in hosts[1:]:
serving_msg += ", http://{0}:{1}/index.html".format(host, port)
#print('Started WebServer, lendingbot status available at {0}'.format(serving_msg))
print(('Started WebServer, Roy Trader status available at {0}'.format(serving_msg)))
server.serve_forever()
time.sleep(5)
except Exception as ex:
print(('Failed to start WebServer: {0}'), ex)
#pass
finally:
subprocess.Popen(cmdkill, stdout=subprocess.PIPE, shell=True)
def stop_web_server():
'''
Stop the web server
'''
try:
print("Stopping WebServer")
threading.Thread(target=server.shutdown).start()
except Exception as ex:
#raise ex
#print("Failed to stop WebServer: {0}".format(ex.message))
print(('Failed to start WebServer: {0}'), ex)
finally:
subprocess.Popen(cmdkill, stdout=subprocess.PIPE, shell=True)