使用 tor 向 python 发出请求?

Sid*_*Sid 6 tor python-3.x python-requests

我是使用requests和的完全新手tor

我正在使用的代码:

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:9050',
                   'https': 'socks5://127.0.0.1:9050'}
    return session

session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
Run Code Online (Sandbox Code Playgroud)

我不断收到以下错误:

raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: SOCKSHTTPConnectionPool(host='httpbin.org', port=80): url 超出最大重试次数:/ip (由 NewConnectionError(': 无法建立新连接引起)连接:[WinError 10061]无法建立连接,因为目标机器主动拒绝它',))

我查看了各种答案,但无法找出问题所在。

jam*_*ell 4

这是适合我的代码:

import requests
torport = 9050
proxies = {
    'http': "socks5h://localhost:{}".format(torport),
    'https': "socks5h://localhost:{}".format(torport)
}

print(requests.get('http://icanhazip.com', proxies=proxies).content)
Run Code Online (Sandbox Code Playgroud)

检查并确保 Tor 正在您的计算机上运行,​​并且 Windows/Defender 等上的防火墙没有阻止您尝试访问的端口。