Created
July 14, 2013 16:14
-
-
Save wolph/5994788 to your computer and use it in GitHub Desktop.
Gevent based proxy server for non http compliant webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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