# -*- encoding: utf-8 -*- import urlparse from cStringIO import StringIO import time def html_esc(s): return s.replace('&','&').replace('<','<').replace('>','>') def do_response(req, body, code=200, headers={}): req.send_response(code) req.send_header('Content-Length', str(len(body))) if 'Content-Type' not in headers: req.send_header('Content-Type', 'text/html') for k, v in headers.items(): if k != 'Content-Length': req.send_header(k, v) req.end_headers() return StringIO(body) def do_redirect(req, target): return do_response(req, 'Go here'.format(target), code=302, headers={ 'Location': target }) def handle_request(req): url = urlparse.urlparse(req.path) # This handler returns one of several different documents, # depending on the query string. Many of the URLs involved contain # text encoded in Shift_JIS, and will not round-trip correctly if # misinterpreted as UTF-8. Comments indicate the Unicode equivalent. if url.query == '/': return do_redirect(req, '?/%83y%81[%83W') elif url.query == '/f': return do_response(req, '' '
URL not found: {}
' .format(html_esc(req.path)), code=404)