Skip to content

Instantly share code, notes, and snippets.

@ikatson
Created May 30, 2013 21:25
Show Gist options
  • Save ikatson/5681365 to your computer and use it in GitHub Desktop.
Save ikatson/5681365 to your computer and use it in GitHub Desktop.
import os
import uwsgi
import gevent
import gevent.select
import gevent.socket
class ProxyMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
if environ['REQUEST_METHOD'] == 'CONNECT':
print 'Environ', environ
host, port = environ['PATH_INFO'].split(':')
port = int(port)
s = gevent.socket.socket()
s.connect((host, port))
client_fd = uwsgi.connection_fd()
server_fd = s.fileno()
uwsgi.send('HTTP/1.0 200 Connection established\r\n\r\n')
while True:
# tcpdump shows 167 bytes of data coming from client_fd here
# but this select call never returns anything in the readlist.
rlist = gevent.select.select([client_fd, server_fd], [], [], 1)[0]
print rlist
if client_fd in rlist:
s.send(uwsgi.recv(4096))
if server_fd in rlist:
uwsgi.send(s.recv(4096))
return self.app(environ, start_response)
application = ProxyMiddleware(None)
# run with
# uwsgi --wsgi proxy_wsgi --gevent 100 --http 127.0.0.1:8081 --home [virtualenv with gevent]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment