-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpserver.py
More file actions
57 lines (45 loc) · 1.62 KB
/
httpserver.py
File metadata and controls
57 lines (45 loc) · 1.62 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
47
48
49
50
51
52
53
54
55
56
57
import http
from threading import Thread
from http.server import BaseHTTPRequestHandler
import json
class HTTPServer(Thread):
def __init__(self, packet_queue):
Thread.__init__(self)
self.packet_queue = packet_queue
def run(self):
server_address = ("localhost", 8080)
handlerClass = HTTPHandler
handlerClass.queue = self.packet_queue
httpd = http.server.HTTPServer(server_address, handlerClass)
httpd.serve_forever()
class HTTPHandler(BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def do_HEAD(self):
return
def do_GET(self):
self.respond()
def do_POST(self):
return
def handle_http(self, status, content_type):
if self.path == "/":
status = 200
content_type = "text/html"
response_content = open("index.html")
response_content = response_content.read()
elif self.path == "/data.json" or self.path == "/data":
status = 200
content_type = "text/json"
packets = [packet.__dict__ for packet in self.queue.queue]
response_content = json.dumps(packets)
else:
status = 404
content_type = "text/plain"
response_content = "404 Not Found"
self.send_response(status)
self.send_header('Content-type', content_type)
self.end_headers()
return bytes(response_content, "UTF-8")
def respond(self):
content = self.handle_http(200, "text/html")
self.wfile.write(content)