Mailing List Archive

How do I use proxies with httplib?
I want to use httplib through a proxy server and I can't seem to get
it to work. Someone in this group suggested setting an environment
variable, like:

SET http_proxy="12.189.130.200:80"

This didn't help. Here is a sample session:


Python 1.5.1 (#0, Nov 18 1998, 12:17:58) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import httplib
>>> h=httplib.HTTP('www.yahoo.com')

Traceback (innermost last): File "<stdin>", line 1, in ? File "C:\PROGRAM
FILES\PYTHON\lib\python1.5\httplib.py", line 51, in __init__ if host:
self.connect(host, port) File "C:\PROGRAM
FILES\PYTHON\lib\python1.5\httplib.py", line 79, in connect
self.sock.connect(host, port) File "<string>", line 1, in connect
socket.error: host not found

>>>

For any given test site, I have no problem with a normal web browser,
but I can't get past this error with Python. Am I missing something
obvious?

Thanks,
- Bruce

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
How do I use proxies with httplib? [ In reply to ]
>I want to use httplib through a proxy server and I can't seem to get
>it to work. Someone in this group suggested setting an environment
>variable, like:
>
>SET http_proxy=3D"12.189.130.200:80"

I don't know that that will work for httplib.
I know it *does* work with urllib.=20
An example from memory (not tested):

import os, urllib
os.environ['http_proxy']=3D"http://12.189.130.200:80"
f =3D urllib.urlopen('http://www.python.org')
data =3D f.read()

--Jeff
How do I use proxies with httplib? [ In reply to ]
<befletch@my-dejanews.com> wrote:
> I want to use httplib through a proxy server and I can't seem to get
> it to work.

that's probably because it doesn't work:

[Lib]$ grep proxy httplib.py
[Lib]$

you might be able to use urllib instead:

[Lib]$ grep proxy urllib.py
proxy = self.proxies[type]
type, proxy = splittype(proxy)
host, selector = splithost(proxy)
#print "proxy via http:", host, selector
"""Return a dictionary of scheme -> proxy server URL mappings.
proxies. An HTTP proxy, for instance, is stored under
"""Return a dictionary of scheme -> proxy server URL mappings.
Scan the environment for variables named <scheme>_proxy;
if value and name[-6:] == '_proxy':
[Lib]$

</F>
How do I use proxies with httplib? [ In reply to ]
In article <099101be8c3f$cb6206e0$f29b12c2@pythonware.com>,
"Fredrik Lundh" <fredrik@pythonware.com> wrote:
> <befletch@my-dejanews.com> wrote:
> > I want to use httplib through a proxy server and I can't seem to get
> > it to work.
[...]
> you might be able to use urllib instead:
[...]

When I first looked at this I thought it wouldn't do the trick either,
since I wanted to use the HTTP POST protocol. On further inspection
I see that urllib has that covered. So I tried it out, only to run up
against a problem that I want to blame on urllib; it claims to not
recognize the http url type:

Python 1.5.1 (#0, Nov 18 1998, 12:17:58) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import urllib
>>> connection=urllib.urlopen('http://www.yahoo.com')

Traceback (innermost last): File "<stdin>", line 1, in ? File "C:\PROGRAM
FILES\HOMERUN\lib\python1.5\urllib.py", line 59, in urlopen return
_urlopener.open(url) File "C:\PROGRAM
FILES\HOMERUN\lib\python1.5\urllib.py", line 155, in open return
self.open_unknown(fullurl) File "C:\PROGRAM
FILES\HOMERUN\lib\python1.5\urllib.py", line 169, in open_unk nown raise
IOError, ('url error', 'unknown url type', type) IOError: ('url error',
'unknown url type', 'http')

>>>

As a test, I hacked urllib by forcing it to think all url's are http
url's, like so:

# name = 'open_' + type
name = 'open_http'

This gets past the url type only to fail on the urllib line:

if not host: raise IOError, ('http error', 'no host given')

Hacking in a host doesn't help much either. Is there something wrong
with my proxy 'set' command? (This is under W95)

SET http_proxy="100.287.14.130:80"

Thanks again,
- Bruce

(emailed & posted)

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
How do I use proxies with httplib? [ In reply to ]
Proxy configuration with urllib in Python 1.5.1 is broken. If my memory
serves correctly it gives just the error you describe. Upgrade to the
newly-hatched 1.5.2 and everything should be peachy.

You can download it from:
http://www.python.org/1.5/

On a completely different note: can anyone out there explain what
1.5.1p1 is?

befletch@my-dejanews.com writes:

> In article <099101be8c3f$cb6206e0$f29b12c2@pythonware.com>,
> "Fredrik Lundh" <fredrik@pythonware.com> wrote:
> > <befletch@my-dejanews.com> wrote:
> > > I want to use httplib through a proxy server and I can't seem to get
> > > it to work.
> [...]
> > you might be able to use urllib instead:
> [...]
>
> When I first looked at this I thought it wouldn't do the trick either,
> since I wanted to use the HTTP POST protocol. On further inspection
> I see that urllib has that covered. So I tried it out, only to run up
> against a problem that I want to blame on urllib; it claims to not
> recognize the http url type:
>
> Python 1.5.1 (#0, Nov 18 1998, 12:17:58) [MSC 32 bit (Intel)] on win32
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> import urllib
> >>> connection=urllib.urlopen('http://www.yahoo.com')
>
> Traceback (innermost last): File "<stdin>", line 1, in ? File "C:\PROGRAM
> FILES\HOMERUN\lib\python1.5\urllib.py", line 59, in urlopen return
> _urlopener.open(url) File "C:\PROGRAM
> FILES\HOMERUN\lib\python1.5\urllib.py", line 155, in open return
> self.open_unknown(fullurl) File "C:\PROGRAM
> FILES\HOMERUN\lib\python1.5\urllib.py", line 169, in open_unk nown raise
> IOError, ('url error', 'unknown url type', type) IOError: ('url error',
> 'unknown url type', 'http')
>
> >>>
>
> As a test, I hacked urllib by forcing it to think all url's are http
> url's, like so:
>
> # name = 'open_' + type
> name = 'open_http'
>
> This gets past the url type only to fail on the urllib line:
>
> if not host: raise IOError, ('http error', 'no host given')
>
> Hacking in a host doesn't help much either. Is there something wrong
> with my proxy 'set' command? (This is under W95)
>
> SET http_proxy="100.287.14.130:80"
>
> Thanks again,
> - Bruce
>
> (emailed & posted)
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

--
-chad
How do I use proxies with httplib? [ In reply to ]
>I see that urllib has that covered. So I tried it out, only to run up
>against a problem that I want to blame on urllib; it claims to not
>recognize the http url type:

proxies in urllib for 1.5.1 will work if you apply the two patches found =
on the [now somewhat buried] "Patches for Python 1.5.1" page:
http://www.python.org/1.5/patches-1.5.1/

Or, as Chad suggested, upgrade to 1.5.2

--Jeff