-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.py
41 lines (36 loc) · 1.31 KB
/
httpserver.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
## HTTP Server Code
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
from io import BytesIO
class WebServerHandler(SimpleHTTPRequestHandler):
def do_GET(self):
try:
print(dir(self))
print(self.path)
if self.path.endswith("/hello"):
self.send_response(200, "OK")
self.send_header('Content-type', 'application-json')
self.end_headers()
self.wfile.write(b'Hello World')
except IOError:
self.send_error(401, 'File not found')
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
send_response = self.send_response(200, "OK")
response = send_response
# self.send_header('Content-type', 'application-json')
self.end_headers()
resp = BytesIO()
resp.write(body)
self.wfile.write(resp.getvalue())
def run(server_class=HTTPServer,
handler_class=WebServerHandler):
server_address = ('localhost', 8080)
try:
print("Starting server")
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
except KeyboardInterrupt:
print("Keyboard Interrrupt")
httpd.socket.close()
run()