-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHTTPRequestHandler.py
More file actions
107 lines (83 loc) · 3.39 KB
/
HTTPRequestHandler.py
File metadata and controls
107 lines (83 loc) · 3.39 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
HTTPRequestHandler.py
Author: Toki Migimatsu
Created: April 2017
"""
from __future__ import print_function
import cgi
import sys
import os
import mimetypes
if sys.version.startswith("3"):
from http.server import BaseHTTPRequestHandler
else:
from BaseHTTPServer import BaseHTTPRequestHandler
def makeHTTPRequestHandler(get_callback=None, post_callback=None, callback_args={}):
"""
Factory method to create HTTPRequestHandler class with custom GET and POST callbacks.
Usage:
extra_args = {"random_num": 123}
http_server = HTTPServer(("", args.http_port), makeHTTPRequestHandler(handle_get_request, handle_post_request, extra_args))
http_server.serve_forever()
def handle_get_request(http_request_handler, get_vars, **kwargs):
with open("index.html","rb") as f:
http_request_handler.wfile.write(f.read())
def handle_post_request(http_request_handler, post_vars, **kwargs):
for key, val in post_vars.items():
print("%s: %s" % (key, val))
print(kwargs["random_num"])
"""
class HTTPRequestHandler(BaseHTTPRequestHandler):
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
"": "text/html" # Default
})
def __init__(self, request, client_address, server):
BaseHTTPRequestHandler.__init__(self, request, client_address, server)
def guess_type(self, path):
"""
Guess the mime type of a file.
"""
base, ext = os.path.splitext(path)
if ext in self.extensions_map:
return self.extensions_map[ext]
ext = ext.lower()
if ext in self.extensions_map:
return self.extensions_map[ext]
return self.extensions_map[""]
def set_headers(self):
"""
Return OK message.
"""
self.send_response(200)
self.send_header("Content-type", self.guess_type(self.path))
self.end_headers()
def do_GET(self):
"""
Parse GET request and call get_callback(HTTPRequestHandler, get_vars, **callback_args).
"""
self.set_headers()
# Call get_callback argument
if get_callback is not None:
get_callback(self, None, **callback_args)
def do_POST(self):
"""
Parse POST request and call post_callback(HTTPRequestHandler, post_vars, **callback_args)
"""
self.set_headers()
# Parse post content
content_type, parse_dict = cgi.parse_header(self.headers["Content-Type"])
parse_dict = {key: val.encode("utf-8") for key, val in parse_dict.items()}
if content_type == "multipart/form-data":
post_vars = cgi.parse_multipart(self.rfile, parse_dict)
elif content_type == "application/x-www-form-urlencoded":
content_length = int(self.headers["Content-Length"])
post_vars = cgi.parse_qs(self.rfile.read(content_length), keep_blank_values=1)
else:
post_vars = {}
# Call post_callback argument
if post_callback is not None:
post_callback(self, post_vars, **callback_args)
return HTTPRequestHandler