-
-
Save beugley/13dd4cba88a19169bcb0 to your computer and use it in GitHub Desktop.
import urlparse | |
import httplib | |
import base64 | |
proxy_uri = "http://user:password@proxy_host:proxy_port" | |
host = 'www.google.com' | |
port = 443 | |
url = urlparse.urlparse(proxy_uri) | |
conn = httplib.HTTPSConnection(url.hostname, url.port) | |
headers = {} | |
if url.username and url.password: | |
auth = '%s:%s' % (url.username, url.password) | |
headers['Proxy-Authorization'] = 'Basic ' + base64.b64encode(auth) | |
conn.set_tunnel(host, port, headers) | |
conn.request("GET", "/") | |
response = conn.getresponse() | |
print response.status, response.reason | |
output = response.read() |
This worked for me for Python 3:
headers['Proxy-Authorization'] = 'Basic ' + str(base64.b64encode(auth.encode())).replace("b'", "").replace("'", "")
Hello. Im trying to do this.what is want to understand is where exactly do we specify the actual password to use for authentication. I understand that this connection is done via SSL and that we use base64 encryption but where in the code is the username and password going ro be inserted. Sorry for the ignorance. Thank you.
@victoos22 You need to edit the first line, proxy_uri = "http://user:password@proxy_host:proxy_port", and replace the user:password with your user/password for your proxy.
how do you authenticate the host if a basic authentication is required - i.e if it requires a username and password to have access to the API service
Updated for python3:
https://gist.github.com/79man/146aa167d72865d560a500b64142319f
Apparently, when I run the conn.request(), it times out. This shouldn't be happening. Anyone ever encountered this?
It's still working when I pass the wrong URL password. Shouldn't it give an Authentication exception?
This worked for me for Python 3:
headers['Proxy-Authorization'] = 'Basic ' + str(base64.b64encode(auth.encode()))