-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdummy_handler.py
53 lines (44 loc) · 1.64 KB
/
dummy_handler.py
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
import http.server
import urllib
import traceback
import sys
class DummyHandler(http.server.BaseHTTPRequestHandler):
parser=None #A global variable having an instance of a parser which has a .parse_text(txt) method, set by whoever uses this
def process(self,txt):
try:
resp=self.parser.parse_text(txt)
except:
self.send_response(500,"Internal server error")
self.end_headers()
self.wfile.write(traceback.format_exc().encode("utf-8"))
self.wfile.flush()
self.close_connection=True
return
self.send_response(200, 'OK')
self.send_header("Content-type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(resp.encode("utf-8"))
self.wfile.flush()
self.close_connection=True
def do_GET(self):
try:
data=urllib.parse.urlparse(self.path)
txt=urllib.parse.parse_qs(data.query)["text"][0]
except:
self.send_response(400, 'Bad request, missing ?text=txt')
self.end_headers()
self.close_connection=True
return
self.process(txt)
def do_POST(self):
try:
content_length = int(self.headers['Content-Length'])
txt=self.rfile.read(content_length).decode("utf-8")
sys.stderr.flush()
except:
self.send_response(400, 'Bad request')
self.end_headers()
self.wfile.write(traceback.format_exc().encode("utf-8"))
self.close_connection=True
return
self.process(txt)