Last active
February 22, 2023 12:21
-
-
Save beugley/13dd4cba88a19169bcb0 to your computer and use it in GitHub Desktop.
Python: use httplib with proxy authentication
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 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() |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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