-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
58 lines (49 loc) · 1.92 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
import linecache
import socket
import threading
import sys
from utils import logger
from utils import Response
from utils import ResponseHeaders
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
# client.settimeout(1)
try:
threading.Thread(target=self.listenToClient, args=(client, address)).start()
except Exception as e:
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
error = 'EXCEPTION IN ({}, LINE {} "{}"): {}\n'.format(filename, lineno, line.strip(), exc_obj)
logger.logerror(error)
def listenToClient(self, client, address):
data = client.recv(1024)
while True:
try:
if data:
print(data.decode())
Response.GetResponse(client, data.decode(), True)
break
else:
logger.logerror('Client Disconnected')
raise socket.error('Client disconnected')
except Exception as ex:
ResponseHeaders.ResponseCodes.Send500Response(client, 'text/html', '0')
client.close()
return False
client.close()
if __name__ == "__main__":
print('PythonServer initialized at port 8080...\nListening...')
ThreadedServer('',8080).listen()