[Tutor] urllib2 with a proxy

kbond kbond at free.fr
Sat Nov 6 16:56:58 CET 2004


hello,

I am trying to get access an internet page  using urllib2. I am able to 
do so form home where I do not have proxy but for some reason I am 
unable to do it form my company.

I found many ressource on the internet explaining how to do it but I was 
unable to apply it to my contexte. The interesting thing is that I did 
manage to configure Firefox (my fovourite browser) so I guess  I should 
have all the information to do it by script.
I think I am doing something is the usage of "add_password". Could you 
please explain me what realm and host are? and how can I get them?

You will find bellow my firefox configuration.
It would be great If someone can help me to put all this together.
Thank you for your help

*Code*
import urllib2
# set up authentication info
authinfo = urllib2.HTTPDigestAuthHandler()
#proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
authinfo.add_password(None, "http://google.com", 'userName', 'pwd')  * *
proxy_support = urllib2.ProxyHandler({"http" : "proxy:8080"})
# build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_opener(proxy_support, 
authinfo,urllib2.CacheFTPHandler)
# install it
urllib2.install_opener(opener)
f = urllib2.urlopen('http://google.com')
buf = f.read()
print buf
f.close()

*Error Message*

Traceback (most recent call last):
  File "test.py", line 18, in ?
    f = urllib2.urlopen('http://google.com')
  File "E:\users\install\Python\lib\urllib2.py", line 129, in urlopen
    return _opener.open(url, data)
  File "E:\users\install\Python\lib\urllib2.py", line 326, in open
    '_open', req)
  File "E:\users\install\Python\lib\urllib2.py", line 306, in _call_chain
    result = func(*args)
  File "E:\users\install\Python\lib\urllib2.py", line 491, in <lambda>
    lambda r, proxy=url, type=type, meth=self.proxy_open: \
  File "E:\users\install\Python\lib\urllib2.py", line 498, in proxy_open
    if '@' in host:
TypeError: iterable argument required

*Firefox configuration*




More information about the Tutor mailing list