-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpServer.py
32 lines (28 loc) · 1.18 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
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
query_components = parse_qs(urlparse(self.path).query)
print("Received GET request:")
for key, value in query_components.items():
print(f"{key}: {value[0]}")
self.wfile.write(b"GET request received")
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
print("Received POST request:")
print(post_data.decode('utf-8'))
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"POST request received")
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"Starting server on port {port}...")
httpd.serve_forever()
if __name__ == "__main__":
run()