使用 Tor 的 Python 请求

问题描述 投票:0回答:2

无法将 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)

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

错误:

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

python python-requests tor socks
2个回答
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)

Note

h
socks5h://

此外,您必须在计算机(而不是浏览器)上运行 tor。您可以通过运行 brew install tor 使用

homebrew
安装 tor。

只需在终端中运行

tor
即可启动一个tor实例。


0
投票

drew010的评论应该就是答案

Tor 浏览器侦听端口 9150,而不是 9050(由 tor expert bundle / tor daemon 使用)

© www.soinside.com 2019 - 2024. All rights reserved.