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:[email protected]
HTTPS_PROXY = https://username:[email protected]

But still getting the error .

urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

where do i get case1: username/password/proxyserver.domain.com
You either set the _proxy variables or you use the ProxyHandler. I am not aware what happens if you do both
Didn't resolve the issue , but insights were helpful . Thanks.
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()

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.