【发布时间】:2018-12-04 14:00:48
【问题描述】:
我想使用 python 3 发出匿名网络请求。
我尝试了一些建议,例如:Make requests using Python over Tor
我已经设法使用这个 sn-p 获得了一个假 ip:
安装
pip 安装请求请求[socks]
基本用法
import requests
def get_tor_session():
session = requests.session()
# Tor uses the 9050 port as the default socks port
session.proxies = {'http': 'socks5://127.0.0.1:9150',
'https': 'socks5://127.0.0.1:9150'}
return session
# Make a request through the Tor connection
# IP visible through Tor
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
# Above should print an IP different than your public IP
# Following prints your normal public IP
print(requests.get("http://httpbin.org/ip").text)
但这仅适用于端口 9150 并且当 Tor 网络浏览器工作时。 我想在没有 Tor 浏览器的情况下发出请求,因为我想 Dockerize 整个事情。
我已经阅读了有关 Socks5 的信息,如您所见,我已经安装了它,但是当我在同一个 sn-p 上的端口 9050 上发出请求时,我得到:
requests.exceptions.ConnectionError: SOCKSHTTPConnectionPool(host='httpbin.org', port=80): 最大重试次数 超出 url: /ip (由 NewConnectionError(': 建立新连接失败: [WinError 10061] 无法建立连接,因为目标机器主动 拒绝了',))
我该如何解决?
谢谢!
【问题讨论】:
-
您想使用 Tor 专家包,如下所示:stackoverflow.com/questions/38847434/…
标签: python python-3.x python-requests tor socks