[Tutor] urllib2 with a proxy

Kent Johnson kent_johnson at skillsoft.com
Sat Nov 6 13:33:42 CET 2004


I'm not familiar with this library but looking at your code and the docs I 
can make some guesses where the problem might be:
- The second argument to ProxyHandler should be a complete URL, e.g. 
'http://proxy:8080'. This is the cause of the exception you are seeing.
- Try using a HTTPBasicAuthHandler instead of a HTTPDigestAuthHandler 
unless you are sure your proxy uses digest authentication
- You should pass an instance of CacheFTPHandler to build_opener, not the 
class itself. In other words, pass CacheFTPHandler() with parentheses.

HTH
Kent

At 10:56 AM 11/6/2004 -0500, kbond wrote:
>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*
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list