Search code examples
pythonauthenticationproxyurllib2python-2.6

Proxy authentication error - python


Hi I have written a few simple lines of code. But I seem to be getting a Authentication error. Can anyone please suggest , what credentials are being looked for python here ?

Code:

import urllib2
response = urllib2.urlopen('http://google.com')
html = response.read()

Error

urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required

PS: I do not have acces to IE -->Advanced settings or regedit

As advised I've modified the code :

import urllib2
proxy_support = urllib2.ProxyHandler({'http':r'http://usename:psw@IP:port'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy_support, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
response = urllib2.urlopen('http://google.com')
html = response.read()

Also I have created two environment variables :

HTTP_PROXY = http://username:password@proxyserver.domain.com
HTTPS_PROXY = https://username:password@proxyserver.domain.com

But still getting the error .

urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required


Solution

  • There are multiple ways to work-around your problem. You may want to try defining an environment variables with the names http_proxy and https_proxy with each set to you proxy URL. Refer to this link for more details.

    Alternatively, you may want to explicitly define a ProxyHandler to work with urllib2 while handling requests through the proxy. The link is already present within the comment to your query; however I am including it here for the sake of completeness.

    Hope this helps