can't get urllib2 or httplib to work with tor & privoxy

B

Bob Fnord

Here's my python code:

import httplib, urllib2

proxy_handler = {'http' : 'localhost:8118',
'https' : 'localhost:8118'}

def connect_u2(url = 'http://ipid.shat.net/iponly/'):,
proxied = urllib2.ProxyHandler(proxy_handler)
opnr = urllib2.build_opener(proxied)
opnr.addheaders = [('User-agent', agent)]
rsp = opnr.open(url)
page = rsp.read()
return page

def connect_h(url = 'http://ipid.shat.net/iponly/'):
cnn = httplib.HTTPConnection('127.0.0.1', 8118)
cnn.connect()
cnn.request('GET', url)
rsp = cnn.getresponse()
stderr.write('%u %s\n' % (rsp.status, rsp.reason))
page = rsp.read(500)
cnn.close()
return page

Both methods give me a 503 error, even though this

export http_proxy='http://localhost:8118/'
lynx -dump 'http://ipid.shat.net/iponly/'

works in bash and prints out the IP of a tor exit.

What am I doing wrong in python? (I tried both 'localhost:8118' and
'http://localhost:8118/' in the proxy_handler dict.)
 
C

Chris Angelico

Both methods give me a 503 error...

As a networking geek, my first thought would be to fire up a tiny
little "snoop server" and see what, exactly, the two methods are
doing. (Ignore the HTTPS options as they're more complicated to snoop
on; you're almost certainly going to see exactly the same on the
HTTP.)

A snoop server is simply an HTTP server that dumps its requests to
stdout or a file, utterly raw. You can write one in Python fairly
easily, or use another program; if I'm working on Windows, I'll
generally use my own RosMud MUD client, but you'll be able to whip up
a cross-platform one in half a page of code in any decent high level
language. I recommend you work at the sockets level, rather than using
an HTTP library, unless you have one that can emit the entire request,
exactly as it came from the client.

Once you've found the difference(s) between Lynx and your script, you
can see what's causing the 503 (Service Unavailable) error; it may be
that you need to authenticate with the proxy.

Chris Angelico
 
B

Bob Fnord

Chris Angelico said:
As a networking geek, my first thought would be to fire up a tiny
little "snoop server" and see what, exactly, the two methods are
doing. (Ignore the HTTPS options as they're more complicated to snoop
on; you're almost certainly going to see exactly the same on the
HTTP.)

A snoop server is simply an HTTP server that dumps its requests to
stdout or a file, utterly raw. You can write one in Python fairly
easily, or use another program; if I'm working on Windows, I'll
generally use my own RosMud MUD client, but you'll be able to whip up
a cross-platform one in half a page of code in any decent high level
language. I recommend you work at the sockets level, rather than using
an HTTP library, unless you have one that can emit the entire request,
exactly as it came from the client.

Once you've found the difference(s) between Lynx and your script, you
can see what's causing the 503 (Service Unavailable) error; it may be
that you need to authenticate with the proxy.

Duh, I was doing something else stupid.
Thanks but never mind!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top