Skip to content

Instantly share code, notes, and snippets.

@jpetazzo
Created October 13, 2011 05:29
Show Gist options
  • Save jpetazzo/1283467 to your computer and use it in GitHub Desktop.
Save jpetazzo/1283467 to your computer and use it in GitHub Desktop.
WSGI Proxy App to add HTTP Basic Auth to outbound requests
HTTPUSER = 'mylogin'
HTTPPASS = 'mypassword'
from wsgiproxy.app import WSGIProxyApp
from base64 import encodestring
proxyapp = WSGIProxyApp('http://remote.server.domain.com/')
# Craft Basic Auth header. Don't forget to strip the trailing \n.
HTTPAUTH = 'Basic ' + encodestring(HTTPUSER+':'+HTTPPASS).strip()
# Remove "hop-by-hop" headers (as defined by RFC2613, Section 13)
# since they are not allowed by the WSGI standard.
FILTER_HEADERS = [
'Connection',
'Keep-Alive',
'Proxy-Authenticate',
'Proxy-Authorization',
'TE',
'Trailers',
'Transfer-Encoding',
'Upgrade',
]
def wrap_start_response(start_response):
def wrapped_start_response(status, headers_out):
# Remove "hop-by-hop" headers
headers_out = [(k,v) for (k,v) in headers_out
if k not in FILTER_HEADERS]
return start_response(status, headers_out)
return wrapped_start_response
def application(environ, start_response):
start_response = wrap_start_response(start_response)
environ['HTTP_AUTHORIZATION'] = HTTPAUTH
return proxyapp(environ, start_response)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, application)
print "Serving on port 8000..."
httpd.serve_forever()
[program:authproxy]
command=/home/dotcloud/env/bin/python /home/dotcloud/current/authproxy.py
@jpetazzo
Copy link
Author

jpetazzo commented Nov 7, 2011

While I would be tempted to answer « Yeah, gunicorn is awesome and will be faster ! », I think it won't be true if you only have 1 concurrent request at a time. In that specific case, I guess it's harder to be faster than « while True: handle_request() » :-)

@twidi
Copy link

twidi commented Nov 7, 2011

I my case it's a proxy to solr, so maybe i'll have many requests from web, AND ( i'm sure of that) a lot a update from background workers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment