Created
October 13, 2011 05:29
-
-
Save jpetazzo/1283467 to your computer and use it in GitHub Desktop.
WSGI Proxy App to add HTTP Basic Auth to outbound requests
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
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() |
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
[program:authproxy] | |
command=/home/dotcloud/env/bin/python /home/dotcloud/current/authproxy.py |
I wouldn't be surprised if it were limited to only one connection at a time.
However, it should be pretty easy to switch to e.g. gunicorn.
Should I update the code sample accordingly?
Ok it works fine.
Would gunicorn be faster ?
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() » :-)
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
Is this proxy handle multi connexions ?