problem using http proxy with urllib.py and urllib2.py

Raymond Yee yee at uclink.berkeley.edu
Sat Mar 11 19:48:59 EST 2000


I'm having problems with using python with a proxy server.  (In my case,
my proxy server is another PC on the other end of a WebGear Aviator Pro
wireless connection -- and the proxy server software is WinProxy 2.1 by
Ositis.) I'm running Python 1.52 for Win32 on Win98; PythonWin build
128).

For example, if I run the following code (which is based on urllib
looking in the environment variable for proxy servers -- see
http://x42.deja.com/getdoc.xp?AN=581031376 ):

import os os.environ['http_proxy'] = "http://192.168.247.2:80"

import urllib u = urllib.urlopen("http://www.yahoo.com") print
u.info() print u.read()

I get the following output:

Proxy-agent: Ositis-WinProxy for WebGear Connection: close Pragma:
no-cache Cache-Control: no-cache Content-Type: text/html
Content-Encoding: 7bit Content-Length: 161

<HEAD><TITLE>403 Forbidden</TITLE></HEAD> <BODY><H1>403
Forbidden</H1> <P>The request was not properly formatted.  A
possible security risk detected.</P> </BODY>

I know that a module urllib2.py
(http://the-tech.mit.edu/~jeremy/python/urllib2.py) has been
written to deal with a number of problems in urllib.py.  When I run the
same code (replacing references to urllib replaced by urllib2, I get the
following error:

Traceback (innermost last):
  File "D:\Apps\Python\Pythonwin\pywin\framework\scriptutils.py", line
310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\WINDOWS\Desktop\try.py", line 5, in ?
    u = urllib2.urlopen("http://www.yahoo.com")
  File "D:\Apps\Python\Lib\urllib2.py", line 137, in urlopen
    return _opener.open(url, data)
  File "D:\Apps\Python\Lib\urllib2.py", line 321, in open
    '_open', req)
  File "D:\Apps\Python\Lib\urllib2.py", line 300, in _call_chain
    result = apply(func, args)
  File "D:\Apps\Python\Lib\urllib2.py", line 735, in http_open
    return self.parent.error('http', req, fp, code, msg, hdrs)
  File "D:\Apps\Python\Lib\urllib2.py", line 347, in error
    return apply(self._call_chain, args)
  File "D:\Apps\Python\Lib\urllib2.py", line 300, in _call_chain
    result = apply(func, args)
  File "D:\Apps\Python\Lib\urllib2.py", line 426, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden


What am I doing wrong?  I know that the proxy server works -- I have no
problems using either Netscape or IE with the proxy server set to
192.168.247.2.  Moreover, the following Perl program works:

use LWP::UserAgent;

 $ua = new LWP::UserAgent;
 $ua->proxy(['http'] => 'http://192.168.247.2:80');

 $req = new HTTP::Request 'GET',"http://www.yahoo.com";;

 $res = $ua->request($req);
 print $res->content if $res->is_success;


Any insight would be much appreciated.

Thanks in advance from a Python newbie,

-Raymond Yee





More information about the Python-list mailing list