Wednesday, 13 February 2013

Python 2.7: Proxy Authentication error in Urllib2


When we try to use Urllib2 in Python 2.7 in an institute which requires proxy authentication, we are getting the below error

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


We need to set the environmental variable 
HTTP_PROXY and HTTPS_PROXY.your program will pick up proper authentication method and execute the code.

To set the environmental variable please follow the below steps in windows
start-> right click My Computer -> click properties -> Click Advanced System Setting 
A pop up will open

Click Environment variables.
Environment variables pop up will open. Under User variables in that popup, click New button.
In the  new pop up add New User 
variable name : HTTP_PROXY
variable value: http://username:password@proxyserver:port
repeat the same steps for  HTTPs_PROXY also

Wednesday, 6 February 2013

http_proxy password contains @ issue

When you try to set up an "http_proxy" with password which contains @, the set command won't work.

for example, if your proxy password is pass@word and you are trying to set up http_proxy using
set http_proxy="http://username:pass@word@server:port", then proxy won't set

To resolve this issue, you have to  percent-encode the special characters ie replace @ with %40
 set http_proxy="http://username:pass%40word@server:port"