Tunneling httplib Through a Proxy
Asked Answered
S

3

19

I am trying to figure out how to send data to a server through a proxy. I was hoping this would be possible through tor but being as tor uses SOCKS it apparently isn't possible with httplib (correct me if I am wrong)

This is what I have right now

import httplib
con = httplib.HTTPConnection("google.com")
con.set_tunnel(proxy, port)
con.send("Sent Stuff")

The problem is, it seems to freeze when the tunnel is set. Thanks for your help.

Stanstance answered 4/8, 2012 at 3:52 Comment(2)
What's the value of "proxy"? a name or IP address?Diachronic
IP address. I am trying to use an http proxy in this example.Stanstance
S
18

If you want to use http proxy, it should be like this:

import httplib
conn = httplib.HTTPConnection(proxyHost, proxyPort)
conn.request("POST", "http://www.google.com", params)

If you want to use SOCKS proxy, you can use SocksiPy as in this question: How can I use a SOCKS 4/5 proxy with urllib2?

Socialism answered 4/8, 2012 at 6:24 Comment(2)
Ahh I see, I had it slight off.Stanstance
This behaviour easily brings python into the same league of vague or confusing language features as PHP... darn!Boisleduc
A
6

Looks like the correct answer is:

http://bugs.python.org/issue11448#msg130413

import httplib
con = httplib.HTTPConnection(proxyHost, proxyPort)
con.set_tunnel("www.google.com", 80)
con.send("Sent Stuff")
Around answered 26/11, 2013 at 1:26 Comment(0)
V
2

As a follow-up to Khue Vu's answer, here's a complete example, the details of getting this working with a SOCKS proxy were more complex than expected.

First install PySocks with:

pip install PySocks

Then you need to manually set up your SOCKS proxy after instantiating your HTTPConnection and informing it that it's going to be using a proxy:

from http.client import HTTPConnection
from urllib.parse import urlparse, urlencode

import socks

url = urlparse("http://final.destination.example.com:8888/")

conn = HTTPConnection('127.0.0.1', 9000) # use socks proxy address
conn.set_tunnel(url.netloc, url.port) # remote host and port that you actually want to talk to
conn.sock = socks.socksocket() # manually set socket
conn.sock.set_proxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9000) # use socks proxy address
conn.sock.connect((url.netloc, url.port)) # remote host and port that you actually want to talk to

request_path = "%s?%s" % (url.path, url.query)
conn.request("POST", request_path, post_data)

Note that the imports above are python3.x

Velutinous answered 3/7, 2017 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.