-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (29 loc) · 1.16 KB
/
server.py
File metadata and controls
36 lines (29 loc) · 1.16 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
#importing python server module
import http.server
#wrapping http handler
import socketserver
PORT = 8000
#creating a custom handler by inheriting from SimpleHTTPRequestHandl, lets you customize how GET and POST are handled
class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
#checks url path
if self.path == ('/home'):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"<h1>This is the home page</h1>")
else:
self.send_error("page not found")
def do_POST(self):
if self.path == ('/away'):
self.send_response(200)
self.send_header('content-type', 'text/html')
self.end_headers()
self.wfile.write(b"<h1>POST DONE</h1>")
else:
self.send_error("page not found")
#Run the server
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
print(f"Serving at port {PORT}")
#keeps the server running
httpd.serve_forever()