-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpController.py
38 lines (31 loc) · 1.09 KB
/
httpController.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
from http.server import BaseHTTPRequestHandler, HTTPServer
from io import BytesIO
class HttpController(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><body><h1>HELLO WORLD</h1><body><html>", "utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
response.write(body)
self.wfile.write(response.getvalue())
print(response.getvalue())
def run():
port = 8080
host = "127.0.0.1"
server = HTTPServer((host,port), HttpController)
server.serve_forever()
server.server_close()
# if __name__ == "__main__":
# run()