python 407 error

450 views
Skip to first unread message

Aliabbas Petiwala

unread,
Jan 26, 2012, 12:34:45 PM1/26/12
to wncc...@googlegroups.com
I am unable to figure out where to enter the proxy information in python, i get a 407 proxy error,it seems python does not detect the proxy server automatically as i have uninstalled IE.Please let me know the config for iitb proxy

>>> urllib2.urlopen('http://www.python.org/')

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    urllib2.urlopen('http://www.python.org/')
  File "C:\Python26\lib\urllib2.py", line 131, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 402, in open
    response = meth(req, response)
  File "C:\Python26\lib\urllib2.py", line 515, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python26\lib\urllib2.py", line 440, in error
    return self._call_chain(*args)
  File "C:\Python26\lib\urllib2.py", line 374, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 523, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 407: Proxy Authentication Required
--



Aliabbas Petiwala|
Phd Scholar|Interdisciplinary Program in Education Technology
|IIT
 
 


Saket Choudhary

unread,
Jan 26, 2012, 1:06:28 PM1/26/12
to wncc...@googlegroups.com
import urllib2
proxy = urllib2.ProxyHandler({'http': 'ldap_id:ldap_password.@netmon.iitb.ac.in:80'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
print urllib2.urlopen('http://www.google.com').read()


--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

Pritam Baral

unread,
Jan 26, 2012, 1:41:50 PM1/26/12
to wncc...@googlegroups.com
407 is when the Proxy Authentication went wrong. Here, it means urllib2 guessed the proxy correctly (from the environment variables, if not IE), but not the LDAP ID and password, or the password wasn't urlencoded. A more complete solution would be:

import urllib2
from urllib import quote
ldap_ip = quote('<LDAP_ID>')
ldap_password = quote('<LDAP_PASSWORD>')
proxy = urllib2.ProxyHandler({'http': ldap_id+':'+ldap_password+'@netmon.iitb.ac.in:80'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
print urllib2.urlopen('http://www.google.com').read()

PS: The way urllib guesses the Proxy settings: environment variabes. So I guess changing the environment variable in the python script itself would also do. If not http_proxy (or https_proxy) env values are set, only then does it look into the Windows Registry.

PSS: How come you uninstalled IE, I don't think Windows let's one do that.

Regards,

Chhatoi Pritam Baral

Amol Mandhane

unread,
Jan 27, 2012, 1:47:19 AM1/27/12
to wncc...@googlegroups.com
Set the environment variables..... its the best thing to do in this case...

I have tried both env variables and urllib proxy function.... former
one appears to be connecting faster....

On 1/27/12, Pritam Baral <pri...@pritambaral.co.cc> wrote:
> 407 is when the Proxy Authentication went wrong. Here, it means urllib2
> guessed the proxy correctly (from the environment variables, if not IE),
> but not the LDAP ID and password, or the password wasn't urlencoded. A more
> complete solution would be:
>
> import urllib2
> from urllib import quote
> ldap_ip = quote('<LDAP_ID>')
> ldap_password = quote('<LDAP_PASSWORD>')
> proxy = urllib2.ProxyHandler({'http':
> ldap_id+':'+ldap_password+'@netmon.iitb.ac.in:80'})
> opener = urllib2.build_opener(proxy)
> urllib2.install_opener(opener)
> print urllib2.urlopen('http://www.google.com').read()
>

> PS: The way urllib *guesses* the Proxy settings: environment variabes. So I


> guess changing the environment variable in the python script itself would
> also do. If not http_proxy (or https_proxy) env values are set, only then
> does it look into the Windows Registry.
>
> PSS: How come you uninstalled IE, I don't think Windows let's one do that.
>

> *Regards,*
>
> *Chhatoi Pritam Baral*

>>> *


>>>
>>>
>>> Aliabbas Petiwala| Phd Scholar|Interdisciplinary Program in Education

>>> Technology**|IIT *
>>> **
>>> **
>>> *Bombay|+919664867707 | http://home.iitb.ac.in/~aliabbas/*


>>>
>>>
>>> --
>>> The website for the club is http://stab-iitb.org/wncc
>>> To post to this group, send email to wncc...@googlegroups.com
>>>
>>
>> --
>> The website for the club is http://stab-iitb.org/wncc
>> To post to this group, send email to wncc...@googlegroups.com
>>
>
> --
> The website for the club is http://stab-iitb.org/wncc
> To post to this group, send email to wncc...@googlegroups.com
>


--
Regards,
Amol Mandhane | Co-ordinator | Web n' Coding Club
IIT Bombay

Aliabbas Petiwala

unread,
Jan 26, 2012, 10:55:06 PM1/26/12
to wncc...@googlegroups.com
i updated urllib2.py with that code and it works, no need to set it all the time.

@pritam IE can be un installed in win 7 , google for it, ms allows it in the control panel. Its the best thing to do with a win7.

Aliabbas Petiwala

unread,
Jan 27, 2012, 4:13:36 AM1/27/12
to wncc...@googlegroups.com
which environment variables and where?



Aliabbas Petiwala|
Phd Scholar|Interdisciplinary Program in Education Technology
|IIT
 
 


Saket Choudhary

unread,
Jan 28, 2012, 1:01:09 AM1/28/12
to wncc...@googlegroups.com
On 27 January 2012 12:17, Amol Mandhane <manam...@gmail.com> wrote:
Set the environment variables..... its the best thing to do in this case...

I have tried both env variables and urllib proxy function.... former
one appears to be connecting faster....


How did you benchmark ?
 
Reply all
Reply to author
Forward
0 new messages