-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_server_GET.py
More file actions
38 lines (33 loc) · 1.41 KB
/
Copy pathhttp_server_GET.py
File metadata and controls
38 lines (33 loc) · 1.41 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
from http.server import BaseHTTPRequestHandler
from urllib import parse
class GetHandler(BaseHTTPRequestHandler):
def do_GET(self):
parsed_path = parse.urlparse(self.path)
message_parts = [
"CLIENT VALUES:",
"client_address={} ({})".format(self.client_address, self.address_string()),
"command={}".format(self.command),
"path={}".format(self.path),
"real path={}".format(parsed_path.path),
"query={}".format(parsed_path.query),
"request_version={}".format(self.request_version),
"",
"SERVER VALUES:" "server_version={}".format(self.server_version),
"sys_version={}".format(self.sys_version),
"protocol_version={}".format(self.protocol_version),
"",
"HEADERS RECEIVED:",
]
for name, value in sorted(self.headers.items()):
message_parts.append("{}={}".format(name, value.rstrip()))
message_parts.append("")
message = "\r\n".join(message_parts)
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(message.encode("utf-8"))
if __name__ == "__main__":
from http.server import HTTPServer
server = HTTPServer(("localhost", 8080), GetHandler)
print("Starting server, use <Ctrl-C> to stop")
server.serve_forever()