-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
executable file
·72 lines (58 loc) · 2.06 KB
/
example.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import cStringIO
import socket
from eventlet import api, wsgi
def fixup(parser):
env = parser.environ
for key in env.keys():
if key.startswith('HTTP_'):
env[key.split(':')[0].upper().replace('-', '_')] = env.pop(key)
body = env.pop('REQUEST_BODY', '')
cl = env.pop('HTTP_CONTENT_LENGTH', '0')
env['CONTENT_LENGTH'] = cl
env['wsgi.input'] = cStringIO.StringIO(body)
return env
import pyhttp11
h = pyhttp11.HttpParser()
print h.execute("POST http://foo.com/ HTTP/1.1\r\nHost: foo.com\r\nContent-length: 12\r\n\r\nHello, World")
print h.has_error()
print h.is_finished()
fixup(h)
print h.environ
class MongrelProtocol(wsgi.HttpProtocol):
def handle_one_request(self):
if self.server.max_http_version:
self.protocol_version = self.server.max_http_version
buff = ''
while True:
try:
buff += self.request.recv(4096)
h.reset()
h.execute(buff)
if not h.has_error() and h.is_finished():
break
except socket.error, e:
self.close_connection = 1
return
self.environ = fixup(h)
self.requestline = "%s %s %s" % (
self.environ['REQUEST_METHOD'],
self.environ['REQUEST_URI'],
self.environ['HTTP_VERSION'])
self.application = self.server.app
try:
self.server.outstanding_requests += 1
try:
self.handle_one_response()
except socket.error, e:
# Broken pipe, connection reset by peer
if e[0] in (32, 54):
pass
else:
raise
finally:
self.server.outstanding_requests -= 1
def app(env, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
return ["Hello, world\r\n"]
wsgi.server(api.tcp_listener(('', 8889)), app, file('/dev/null', 'w'), protocol=MongrelProtocol)
#wsgi.server(api.tcp_listener(('', 8889)), app, file('/dev/null', 'w'))