3

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

misguided
  • 3,699
  • 21
  • 54
  • 96
  • possible duplicate of [Proxy Authentication error in Urllib2 (Python 2.7)](http://stackoverflow.com/questions/12520790/proxy-authentication-error-in-urllib2-python-2-7) – ranendra Aug 20 '13 at 06:00

3 Answers3

5

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

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
1

If your OS is windows and behind ISA proxy, urllib2 does not use any information about proxy; instead "Firewall Client for ISA server" automatically authenticates the user. That means we don't need to set http_proxy and https_proxy system environment variables. Keep it empty in ProxyHandler as following:

proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

u = urllib2.urlopen('your-url-goes-here')
data = u.read()
sp1der
  • 176
  • 3
-1

The error code and message seem that the username and password failed to pass the authentications of proxy servers.

The following code:

proxy_handler = urllib2.ProxyHandler({'http': 'usename:psw@IP:port'})
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
response = urllib2.urlopen('http://google.com')
html = response.read()

should also works if the authentication is passed.