-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexercise2.py
More file actions
executable file
·38 lines (29 loc) · 1 KB
/
exercise2.py
File metadata and controls
executable file
·38 lines (29 loc) · 1 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
37
38
"""
Exercise #2
Use the form in exercise2_form.html to trigger the request
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qsl
class myHTTPServer_RequestHandler(BaseHTTPRequestHandler):
# GET
def do_GET(self):
# Send response status code
self.send_response(200)
# Send headers
self.send_header('Content-type', 'text/html')
self.end_headers()
# Input parameters (as a dict)
parsed = urlparse(self.path)
params = parse_qsl(parsed.query)
# Concatenate variable-value pairs into a single string
msg = "".join(["{}: {}\n".format(p[0], p[1]) for p in params])
# Write message content as utf-8 data
self.wfile.write(bytes(msg, "utf8"))
return
def main():
server_address = ('127.0.0.1', 8080)
httpd = HTTPServer(server_address, myHTTPServer_RequestHandler)
print("running server...")
httpd.serve_forever()
if __name__ == "__main__":
main()