Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created December 17, 2011 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/1490895 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/1490895 to your computer and use it in GitHub Desktop.
Add proxy authorization to requests
import requests
from base64 import b64encode
proxy = {
'http': 'http://173.208.208.74:60099'
}
class HTTPProxyAuth(requests.auth.HTTPBasicAuth):
"""Like requests.auth.HTTPBasicAuth, but adds a Proxy-Authorization header"""
def __call__(self, r):
auth_s = b64encode('%s:%s' % (self.username, self.password))
r.headers['Proxy-Authorization'] = ('Basic %s' % auth_s)
return r
auth = HTTPProxyAuth('user', 'password')
r = requests.get('http://httpbin.org/', proxies=proxy, return_response=False)
r = auth(r)
r.send()
print r.response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment