diff options
author | Diego Roversi <diegor@tiscali.it> | 2019-09-15 16:24:12 +0200 |
---|---|---|
committer | Diego Roversi <diegor@tiscali.it> | 2019-09-15 16:24:12 +0200 |
commit | 5ac7c2575ec24ca8816953d5254256f54c255f3d (patch) | |
tree | d7d34a409a826b5b6a0c3ffc164e376b5f46f01a | |
parent | bfc996aafb95832a816c2342df5029d099eee2ad (diff) |
convert string into bytes, probably it needs more fix alike
-rw-r--r-- | common/httpserver.py | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/common/httpserver.py b/common/httpserver.py index a01a017..b7d7f70 100644 --- a/common/httpserver.py +++ b/common/httpserver.py @@ -4,7 +4,7 @@ import http.server from http.server import SimpleHTTPRequestHandler import urllib.parse, cgi, html.entities import sys, os, time -from io import StringIO +from io import BytesIO, StringIO class Translator: @@ -22,6 +22,7 @@ class Translator: self.locals = locals def __getitem__(self, expr): + # DEBUG: print(f"expr = {expr}",file=sys.stderr) if '\n' in expr: if not expr.endswith('\n'): expr += '\n' @@ -53,7 +54,7 @@ class TranslatorIO: def generate(self, fmt, d): t = Translator(d, d) for data in fmt.split('\x0c'): - print(f"data={data}",file=sys.stderr) # only for debug + # DEBUG: print(f"data={data}",file=sys.stderr) # only for debug yield (data % t).encode() @@ -64,7 +65,7 @@ for key, value in list(html.entities.entitydefs.items()): text_to_html[value] = '&' + key + ';' def htmlquote(s): - return ''.join([text_to_html.get(c, c) for c in s]) + return ''.join([text_to_html.get(c, c) for c in s]).encode() # HTTP Request Handler @@ -125,19 +126,23 @@ class MiniHandler(SimpleHTTPRequestHandler): hdr['remote host'] = self.client_address[0] f, ctype = loader(headers=hdr, **kwds) except IOError as e: - self.send_error(404, "I/O error: " + str(e)) + print( e, file=sys.stderr ) + self.send_error(404, b"I/O error: " + str(e).encode()) return None except HTTPRequestError as e: - self.send_error(500, str(e)) + print( e, file=sys.stderr ) + self.send_error(500, str(e).encode()) return None except: f = StringIO() import traceback + traceback.print_exc(file=sys.stderr) traceback.print_exc(file=f) data = htmlquote(f.getvalue()) - data = data.replace('\n', '<br>\n') + data = data.replace(b'\n', b'<br>\n') self.send_error(500) - return StringIO('<hr><p>'+data+'</p>') + return BytesIO(b'<hr><p>'+data+b'</p>') + if ctype is None: ctype = self.guess_type(self.translate_path(self.path)) elif f is None: @@ -153,12 +158,13 @@ class MiniHandler(SimpleHTTPRequestHandler): self.send_header("Content-type", 'text/html') self.send_header("Location", url) self.end_headers() - return StringIO('''<html><head></head><body> + return BytesIO(b'''<html><head></head><body> Please <a href="%s">click here</a> to continue. </body></html> -''' % url) +''' % url.encode()) def do_POST(self): + print("do_POST", sys.stderr) try: nbytes = int(self.headers.getheader('content-length')) except: |