Skip to content

Instantly share code, notes, and snippets.

@wolph
Created July 14, 2013 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/5994788 to your computer and use it in GitHub Desktop.
Save wolph/5994788 to your computer and use it in GitHub Desktop.
Gevent based proxy server for non http compliant webserver
from gevent.local import local
from gevent.pywsgi import WSGIServer
from gevent import monkey
import socket
import pprint
import sys
def application(environ, start_response):
url = environ['PATH_INFO']
if url.endswith('/') or url.endswith('.html'):
content_type = 'text/html'
elif url.endswith('.cab'):
content_type = 'application/vnd.ms-cab-compressed'
elif url.endswith('.png'):
content_type = 'image/png'
elif url.endswith('.gif'):
content_type = 'image/gif'
elif url.endswith('.jpeg'):
content_type = 'image/jpeg'
elif url.endswith('.css'):
content_type = 'text/css'
elif url.endswith('.favicon'):
content_type = 'image/x-icon'
elif url.endswith('.js'):
content_type = 'application/javascript'
else:
raise RuntimeError('Unknown mimetype for %r' % url)
start_response(
'200 OK',
[('Content-Type', content_type)]
)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('1.2.3.4', 80))
s.sendall('GET %(PATH_INFO)s HTTP/1.0\r\n' % environ)
data = s.recv(1024)
while data:
sys.stdout.write('.')
if data.startswith('HTTP'):
data = data.split('\r\n\r\n', 1)[1]
yield data
data = s.recv(1024)
socket.setdefaulttimeout(2.0)
WSGIServer(('', 8080), application).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment