通常,当使用httplib(python 2.x)或http.client(python 3.x)时,您设置连接并发出如下请求:
from http.client import HTTPConnection
from urllib.parse import urlparse, urlencode
url = urlparse("http://final.destination.example.com:8888/")
conn = HTTPConnection(url.netloc)
request_path = "%s?%s" % (url.path, url.query)
conn.request("POST", request_path, post_data)
(请注意,此示例代码具有python 3.x导入)
出于性能原因,我希望使用http.client,但我还想使用SOCKS代理远程访问群集。
有没有办法在httplib / http.client中使用SOCKS代理?
答案 0 :(得分:1)
我在这里回答我自己的问题,希望这种情况下的下一个人能够找到解决方案。
首先安装PySocks:
pip install PySocks
然后,您需要在实例化HTTPConnection并通知它将使用代理后手动设置SOCKS代理:
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)