Search code examples
pythonproxyhttplib

python httplib: connect through proxy with authentification


I am trying to send GET request through a proxy with authentification. I have the following existing code:

import httplib

username = 'myname'
password = '1234'
proxyserver = "136.137.138.139"
url       = "http://google.com"
c         = httplib.HTTPConnection(proxyserver, 83, timeout = 30)
c.connect()  
c.request("GET", url)
resp = c.getresponse()
data = resp.read()
print data

when running this code, I get an answer from the proxy saying that I must provide authentification, which is correct. In my code, I don't use login and password. My problem is that i don't know how to use them !

Any idea ?


Solution

  • You can refer this code if you specifically want to use httplib. https://gist.github.com/beugley/13dd4cba88a19169bcb0

    But you could also use the easier requests module.

    import requests
    proxies = {
        "http": "http://username:password@proxyserver:port/",
        # "https": "https://username:password@proxyserver:port/",
    }
    url = 'http://google.com'
    data = requests.get(url, proxies=proxies)