Proxy Authentication Error in Urllib2 (Python 2.7)

[Windows 7 64 bit; Python 2.7]

If I try to use Urllib2, I get this error

Traceback (most recent call last): File "C:\Users\cYanide\Documents\Python Challenge\1\1.py", line 7, in <module> response = urllib2.urlopen('http://python.org/') File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open response = meth(req, response) File "C:\Python27\lib\urllib2.py", line 513, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python27\lib\urllib2.py", line 438, in error return self._call_chain(*args) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(*args) File "C:\Python27\lib\urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) **urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required** 

Now I am behind a college proxy that requires authentication, so that is probably the reason this is happening. But shouldn't Urllib2 output authentication and proxy information from system settings?

I understand that there is some additional code that I can insert into my program to have β€œhardcode” proxy information in the program, but I really do not want to do this if this is not the last resort. This will prevent portability of the program on computers with different authentication identifiers and college passwords.

+4
source share
1 answer

Your program should see the environment variables that are installed on Windows. So these are the two environment variables in your Windows.

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

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

And continue to execute the script. He must pick up the corresponding authenticators and continue the connection.

+2
source

Source: https://habr.com/ru/post/1435380/


All Articles