diff options
Diffstat (limited to 'scripts/async-http.py')
-rw-r--r-- | scripts/async-http.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/scripts/async-http.py b/scripts/async-http.py index 2611ea1b..a22a53a3 100644 --- a/scripts/async-http.py +++ b/scripts/async-http.py @@ -25,6 +25,7 @@ class http_server(asynchat.async_chat): def __init__(self, conn): asynchat.async_chat.__init__(self, conn = conn) + self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.ibuffer = [] self.set_terminator("\r\n\r\n") @@ -36,7 +37,6 @@ class http_server(asynchat.async_chat): def collect_incoming_data(self, data): """Buffer the data""" - print "Got:", data self.ibuffer.append(data) def get_ibuffer(self): @@ -51,13 +51,9 @@ class http_server(asynchat.async_chat): return self.handle_body() raise RuntimeError - def handle_accept(self): - print "handle_accept()" - self.accept() - def handle_headers(self): - request, headers = self.get_ibuffer().split("\r\n", 1) - request = request.split() + headers = self.get_ibuffer().split("\r\n") + request = headers.pop(0).split() if len(request) == 3: cmd, path, version = request assert version[:5] == "HTTP/" @@ -68,32 +64,43 @@ class http_server(asynchat.async_chat): else: raise RuntimeError cmd = cmd.upper() - print headers - headers = email.Parser().parsestr(headers, True) + for i in xrange(len(headers) - 2, -1, -1): + if headers[i + 1][0].isspace(): + headers[i] += headers[i + 1] + del headers[i + 1] + headers = [h.split(":", 1) for h in headers] + headers = dict((k.lower(), v) for (k,v) in headers) print print "Command: ", cmd print "Path: ", path print "Version:", version print print "Headers:", repr(headers) + self.push_line("HTTP/1.0 200 Yo!") + self.push_line("Content-Type: text/plain") + self.push_line() + self.push_line("Hi, Mom!") + self.close_when_done() + #asyncore.close_all() + + def push_line(self, line = ""): + self.push(line + "\r\n") def handle_error(self): print traceback.format_exc() asyncore.close_all() - class http_listener(asyncore.dispatcher): def __init__(self): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.bind(("", 8000)) + self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.listen(5) def handle_accept(self): - print "handle_accept()" - newsock = self.accept() - server = http_server(newsock[0]) + server = http_server(self.accept()[0]) def handle_error(self): print traceback.format_exc() |