urllib2, proxy, basic authentication
Jeremy Lamplough
jeremy.lamplough at motorola.com
Tue Dec 5 16:44:33 EST 2000
try setting the proxy host prior to importing the other modules.
import os, sys
os.environ['http_proxy'] = 'http://[PROXY_HOST]:5865'
import urllib2
from urllib2 import urlopen
The way i'm doing basic stuff, with NO authentication is (to download a
stock quote)
import urllib
import sys
import string
http_proxy = 'http://proxy.mycom...'
opener = urllib.FancyURLopener({'http' : http_proxy})
stocks = sys.argv[1:]
for stock in stocks:
file = opener.open("http://finance.yahoo.com/d/quotes.csv?s=" + stock
+ "&f=sl1d1t1c1ohgv&e=.csv")
print string.strip(file.read())
hope this helps,
jeremy
Albert Chin-A-Young wrote:
>
> I'm trying to use urlopen in urllib2.py with an HTTP proxy to connect
> to a site requiring BASIC authentication. My code follows:
>
> import os, sys, urllib2
> from urllib2 import urlopen
>
> os.environ['http_proxy'] = 'http://[PROXY_HOST]:5865'
>
> authinfo = urllib2.HTTPBasicAuthHandler ()
> authinfo.add_password ('[REALM]', 'http://[URL]/', '[LOGIN]',
> '[PASSWORD]')
>
> opener = urllib2.build_opener (authinfo)
> urllib2.install_opener (opener)
>
> url = urlopen ('http://[URL]/')
> print url.info ()
> url.close ()
>
> This is not working. Does urllib2 support this configuration?
>
> File "/tmp/a.py", line 15, in ?
> url = urlopen ('http://updates.thewrittenword.com/')
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 137, in urlopen
> return _opener.open(url, data)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 325, in open
> '_open', req)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 304, in _call_chain
> result = func(*args)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 764, in http_open
> return self.parent.error('http', req, fp, code, msg, hdrs)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 351, in error
> return self._call_chain(*args)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 304, in _call_chain
> result = func(*args)
> File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
> line 430, in http_error_default
> raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
> urllib2.HTTPError: HTTP Error 401: Authorization Required
>
> --
> albert chin (china @at@ thewrittenword .dot. com)
More information about the Python-list
mailing list