Created
December 17, 2011 18:05
-
-
Save chrisguitarguy/1490895 to your computer and use it in GitHub Desktop.
Add proxy authorization to 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
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