tags:

views:

51

answers:

2

The following piece of code works as expected when running in a local install of django apache 2.2

 fx = urllib2.Request(f);
 fx.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.36 Safari/525.19');
 url_opened = urllib2.urlopen(fx);

However when I enter that code into IDLE on the same machine I get the following error:

    url_opened = urllib2.urlopen(fx);
  File "C:\Python25\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 387, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 425, in error
    return self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 506, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 407: Proxy Authentication Required

Any ideas?

A: 

Maybe the Django version has already prepped urllib2 with the needed credentials for the proxy, while the IDLE version hasn't?

Alex Martelli
+1  A: 

urllib and urllib2 I think look at environment variables for proxies if one isn't set programatically. Maybe the proxy environment variables haven't been set properly in IDLE?

Compare the output of the following from IDLE to the Django program:

import os, pprint
for k in os.environ:
    if 'proxy' in k.lower(): # look for proxy environment variables
        print k, os.environ[k]

EDIT: Quoting http://docs.python.org/library/urllib2.html#urllib2.ProxyHandler:

Cause requests to go through a proxy. If proxies is given, it must be a 
dictionary mapping protocol names to URLs of proxies. The default is to read the 
list of proxies from the environment variables. If no proxy environment 
variables are set, in a Windows environment, proxy settings are obtained from 
the Internet Settings section and in a Mac OS X environment, proxy 
information is retrieved from the OS X System Configuration Framework.

To disable autodetected proxy pass an empty dictionary.

Maybe Django creates a ProxyHandler? Try calling urllib2.ProxyHandler() in IDLE.

David Morrissey
Gave that a whirl and both come back blank.
danspants
Bingo! Thanks for that!
danspants
Interestingly it worked once and i can't replicate it.
danspants
Hmm. Are you still using the same IDLE instance? Try restarting IDLE if you haven't and try it again. Maybe you can only call `ProxyHander()` once? I'm not sure :-P
David Morrissey
It's very strange it seems to randomly work, ahh well i plug away at it and see if i can work out the secret :) Thanks for all your help!
danspants
So it seems my code will work for a while (hitting the same url in a loop i get about 140 iterations) and then the proxy fails. If i then refresh my browser it works again. If I could work out why perhaps i can duplicate the effect within python to refresh the proxy authentication.
danspants
if you're using it at work or something, number of items or amount you can download in a given period might be limited - you might need to use `time.sleep()` every so often and retry when it fails.
David Morrissey