-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexercise1.py
More file actions
50 lines (38 loc) · 1.19 KB
/
Copy pathexercise1.py
File metadata and controls
50 lines (38 loc) · 1.19 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
39
40
41
42
43
44
45
46
47
48
49
50
"""
Exercise #1
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
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()
message = "Hello world!"
# Write message content as utf-8 data
self.wfile.write(bytes(message, "utf8"))
return
# POST
def do_POST(self):
# Send response status code
self.send_response(200)
# Send headers
self.send_header('Content-type', 'text/html')
self.end_headers()
# Gets the size of data
l = int(self.headers.get("Content-length"))
# Gets the data itself (byte string)
vars = self.rfile.read(l)
# Write message content
self.wfile.write(bytes("POST world!", "utf8"))
self.wfile.write(vars)
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()