|
|
@ -2,9 +2,9 @@ |
|
|
|
Basic web server / framework. |
|
|
|
Basic web server / framework. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
from SocketServer import ThreadingMixIn |
|
|
|
from socketserver import ThreadingMixIn |
|
|
|
import BaseHTTPServer |
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler |
|
|
|
import urlparse |
|
|
|
import urllib.parse |
|
|
|
import cgi |
|
|
|
import cgi |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -14,6 +14,9 @@ class HTTPError(Exception): |
|
|
|
etc. They are caught by the 'framework' and sent to the client's browser. |
|
|
|
etc. They are caught by the 'framework' and sent to the client's browser. |
|
|
|
""" |
|
|
|
""" |
|
|
|
def __init__(self, status_code, msg, headers=None): |
|
|
|
def __init__(self, status_code, msg, headers=None): |
|
|
|
|
|
|
|
assert isinstance(status_code, int) |
|
|
|
|
|
|
|
assert isinstance(msg, str) |
|
|
|
|
|
|
|
|
|
|
|
if headers is None: |
|
|
|
if headers is None: |
|
|
|
headers = {} |
|
|
|
headers = {} |
|
|
|
self.status_code = status_code |
|
|
|
self.status_code = status_code |
|
|
@ -22,14 +25,14 @@ class HTTPError(Exception): |
|
|
|
Exception.__init__(self, status_code, msg, headers) |
|
|
|
Exception.__init__(self, status_code, msg, headers) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadedHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer): |
|
|
|
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Base class for multithreaded HTTP servers. |
|
|
|
Base class for multithreaded HTTP servers. |
|
|
|
""" |
|
|
|
""" |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
|
|
|
class RequestHandler(BaseHTTPRequestHandler): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Basic web server request handler. Handles GET and POST requests. You should |
|
|
|
Basic web server request handler. Handles GET and POST requests. You should |
|
|
|
inherit from this class and implement h_ methods for handling requests. |
|
|
|
inherit from this class and implement h_ methods for handling requests. |
|
|
@ -61,9 +64,9 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
|
|
|
""" |
|
|
|
""" |
|
|
|
Parse information from a request. |
|
|
|
Parse information from a request. |
|
|
|
""" |
|
|
|
""" |
|
|
|
url_comp = urlparse.urlsplit(reqinfo) |
|
|
|
url_comp = urllib.parse.urlsplit(reqinfo) |
|
|
|
path = url_comp.path |
|
|
|
path = url_comp.path |
|
|
|
query_vars = urlparse.parse_qs(url_comp.query) |
|
|
|
query_vars = urllib.parse.parse_qs(url_comp.query) |
|
|
|
# Only return the first value of each query var. E.g. for |
|
|
|
# Only return the first value of each query var. E.g. for |
|
|
|
# "?foo=1&foo=2" return '1'. |
|
|
|
# "?foo=1&foo=2" return '1'. |
|
|
|
var_values = dict([(k, v[0]) for k, v in query_vars.items()]) |
|
|
|
var_values = dict([(k, v[0]) for k, v in query_vars.items()]) |
|
|
@ -104,7 +107,7 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
|
|
|
self.send_header(header_k, header_v) |
|
|
|
self.send_header(header_k, header_v) |
|
|
|
self.end_headers() |
|
|
|
self.end_headers() |
|
|
|
self.wfile.write("Error {0}: {1}".format(err.status_code, |
|
|
|
self.wfile.write("Error {0}: {1}".format(err.status_code, |
|
|
|
err.msg)) |
|
|
|
err.msg).encode('utf-8')) |
|
|
|
self.wfile.flush() |
|
|
|
self.wfile.flush() |
|
|
|
return False |
|
|
|
return False |
|
|
|
except Exception as err: |
|
|
|
except Exception as err: |
|
|
|