Skip to content

Instantly share code, notes, and snippets.

@beugley
Last active February 22, 2023 12:21
Show Gist options
  • Save beugley/13dd4cba88a19169bcb0 to your computer and use it in GitHub Desktop.
Save beugley/13dd4cba88a19169bcb0 to your computer and use it in GitHub Desktop.
Python: use httplib with proxy authentication
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()
@salabi
Copy link

salabi commented Jun 14, 2020

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

@79man
Copy link

79man commented Mar 23, 2022

@divyanshukatiyar
Copy link

Apparently, when I run the conn.request(), it times out. This shouldn't be happening. Anyone ever encountered this?

@Rudraansh
Copy link

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