使用 Tor 的 Python 请求

Vax*_*axe 5 python tor socks python-requests

无法将 Tor 与 Python 请求结合使用

import requests
proxies = {
    'http': 'socks5://localhost:9050',
    'https': 'socks5://localhost:9050'
}
url = 'http://httpbin.org/ip'
print(requests.get(url, proxies=proxies).text)
Run Code Online (Sandbox Code Playgroud)

我尝试了多种解决方案,但没有一个对我有用。我正在尝试通过 Tor 使用 Python 发出简单的请求。提前致谢。

错误:

requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='canihazip.com', port=443): url 超出最大重试次数: / (由 NewConnectionError('< urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x031B77F0>: 无法建立blish 一个新连接:[Errno 10061] 无法建立连接,因为目标机器主动拒绝它',))

Rei*_*ica 2

首先确保您pip3 install requests[socks],或者如果使用 zsh,pip3 install "requests[socks]"

然后执行以下操作:

import requests
session = requests.session()
proxies = {
    'http': 'socks5h://localhost:9050',
    'https': 'socks5h://localhost:9050'
}
session.get(url, proxies=proxies)
Run Code Online (Sandbox Code Playgroud)

注意hsocks5h://

此外,您还必须在计算机上运行 Tor(而不是浏览器)。您可以通过运行使用自制软件brew install tor来安装 Tor 。

您只需在终端中运行即可启动 Tor 实例tor