-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode2.py
More file actions
32 lines (26 loc) · 981 Bytes
/
Copy pathcode2.py
File metadata and controls
32 lines (26 loc) · 981 Bytes
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
#!/usr/bin/env python3
import http.server
import socketserver
import os
from urllib.parse import urlparse, parse_qs
class MyHandler(http.server.CGIHTTPRequestHandler):
def do_GET(self):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
if parsed_url.path == "/custom-route":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"<html><body><h1>Hello, this is a custom route!</h1></body></html>")
else:
# Serve files as usual
super().do_GET()
# Set the port number
PORT = 8000
# Set the current working directory to the script's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Create the server with the custom handler
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
print("Server started at port", PORT)
# Start the server
httpd.serve_forever()