urllib.urlopen and proxies

Paul Moore paul.moore at uk.origin-it.com
Tue Mar 6 09:47:57 EST 2001


On Mon, 5 Mar 2001 16:07:42 +0100 , Siggy Brentrup <bsb at winnegan.de>
wrote:

>"Chris Gonnerman" <chris.gonnerman at usa.net> writes:
>
>> It *appears* (from reading the source) that username and password
>> information for
>> the proxy server must be passed as part of the URL:
>> 
>>     http://username:password@my.host.com/index.html
>> 
>
>I suggest using urllib2, changing the proxy setup described in the
>module's docstring to
>
>  proxy_support = urllib2.ProxyHandler({"http" :
>                                        "http://user:pw@proxy:port"})
>
>Untested too, since my proxy doesn't require authorization.

I had a look at urllib2. The module docstring even includes an
example!

----------
import urllib2
 
# set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password('realm', 'host', 'username', 'password')
 
proxy_support = urllib2.ProxyHandler({"http" :
"http://ahad-haam:3128"})
 
# 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://www.python.org/')
----------

The only problem is that I don't know what the 'realm' and 'host'
parameters to authinfo.add_password should be. I need to supply a
username and password for everything...

Paul.




More information about the Python-list mailing list