Skip to content

Instantly share code, notes, and snippets.

@mxey
Created February 26, 2010 21:29
Show Gist options
  • Save mxey/316181 to your computer and use it in GitHub Desktop.
Save mxey/316181 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import collections
import gevent.monkey
import gevent.wsgi
import httplib
class ConnectionCache(collections.defaultdict):
def __missing__(self, key):
return httplib.HTTPConnection(key)
def convert_header(env):
hdr = filter(lambda x: x[0].startswith('HTTP_'), env.items())
return dict(map(lambda x: (x[0][5:].replace('_', '-').title(), x[1]), hdr))
def application(env, start_response):
hdr = convert_header(env)
conn = cache[env['HTTP_HOST']]
body = env.get('wsgi.input').read()
path = env['PATH_INFO']
if 'QUERY_STRING' in env:
path += '?' + env['QUERY_STRING']
conn.request(env['REQUEST_METHOD'], path, body, hdr)
resp = conn.getresponse()
hdr = dict(resp.getheaders())
if 'transfer-encoding' in hdr:
del hdr['transfer-encoding']
start_response('%s %s' % (resp.status, resp.reason), hdr.items())
return resp.read()
cache = ConnectionCache()
gevent.monkey.patch_all()
gevent.wsgi.WSGIServer(('localhost', 8080), application).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment