【问题标题】:Making a request using requests.get through tor proxies but the code is not responding使用 requests.get 通过 tor 代理发出请求,但代码没有响应
【发布时间】:2020-07-07 16:52:53
【问题描述】:

我想使用 Tor 向网页发出多个 GET 请求。我想为每个请求使用不同的ip地址,所以我写了这个小程序

from stem import Signal
from stem.control import Controller
import requests

def change_ip():
    with Controller.from_port(port=9051) as contr:
        contr.authenticate(password='abhishek')
        contr.signal(Signal.NEWNYM)


session=requests.session()

session.proxies={}

session.proxies['http']='socks5://127.0.0.1:9051'
session.proxies['https']='socks5://127.0.0.1:9051'

for i in range(5):
    r=session.get('http://httpbin.org/ip')
    print(r.text)
    change_ip()

使用这个,我提出了多个请求,但这个程序没有显示任何输出,它就像我在这张图片中显示的那样卡住了this is the screenshot of terminal where i run this program and it stucked

但是当我删除 session.proxies 代码区域时,代码正在运行并显示输出,但这对我来说毫无意义,因为我想在每次请求后更改 IP 地址。

【问题讨论】:

  • 嗨,欢迎来到 SO。您能否详细说明它没有响应或显示错误是什么意思?你是如何尝试运行它的?请用详细的解释更新问题。谢谢。
  • 我编辑了这个问题,请帮忙解决这个问题
  • Tor 在端口 9050 上运行代理,而不是 9051。端口9051 仅用于控制/更改 Tor

标签: python python-requests tor


【解决方案1】:

Tor 在端口 9050 上运行代理,而不是 9051。端口9051 仅用于控制/更改 Tor。


发送信号后我还需要几秒钟来获取新 IP。

当我不对所有 url 使用一个会话,而是对正常请求使用一个会话时,效果会更好

requests.get(..., proxies=proxies)

在一个会话中,它有时会为 https://httpbin.org/iphttps://api.ipify.org 提供相同的 IP,但不会为 https://icanhazip.com 提供相同的 IP。

如果我在每个循环中创建新会话,它会正常工作。


没有会话的版本

from stem import Signal
from stem.control import Controller
import requests
import time

def change_ip():
    with Controller.from_port(port=9051) as control:
        control.authenticate(password='password')
        control.signal(Signal.NEWNYM)

proxies = {
    'http':  'socks5://127.0.0.1:9050',
    'https': 'socks5://127.0.0.1:9050',
}    

for i in range(5):

    r = requests.get('https://httpbin.org/ip', proxies=proxies)
    print(r.json()['origin'])
    
    r = requests.get('https://api.ipify.org', proxies=proxies)
    print(r.text)

    r = requests.get('https://icanhazip.com', proxies=proxies)
    print(r.text)

    change_ip()
    time.sleep(5)

带有会话的版本 - 每个循环中的新会话

from stem import Signal
from stem.control import Controller
import requests
import time

def change_ip():
    with Controller.from_port(port=9051) as control:
        control.authenticate(password='password')
        control.signal(Signal.NEWNYM)

for i in range(5):

    session = requests.Session()
    
    session.proxies = {
        'http':  'socks5://127.0.0.1:9050',
        'https': 'socks5://127.0.0.1:9050',
    }    
    
    r = session.get('https://httpbin.org/ip')
    print(r.json()['origin'])

    r = session.get('https://api.ipify.org')
    print(r.text)

    r = session.get('https://icanhazip.com')
    print(r.text)

    change_ip()
    time.sleep(5)

【讨论】:

  • 我已运行此代码并遇到错误 =>requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by NewConnectionError(': 无法建立新连接: 0x01: 一般 SOCKS 服务器故障'))
  • 在浏览器中连接httpbin.org/ip 有问题吗?也许此时只有服务器有问题。或者,也许您必须更新 OpenSSL 才能正确使用 SSL 和 HTTPS?您对其他网址有疑问 - api.ipify.orgicanhazip.com 吗?
  • httpbin.org/ipapi.ipify.org 引发相同的错误,但 icanhazip.com 此站点引发此权限被拒绝错误 => stem.connection.UnreadableCookieFile: Authentication failed: unable to read '/run/tor /control.authcookie' ([Errno 13] Permission denied: '/run/tor/control.authcookie') 但我没有在 tor 控制端口上应用任何密码
  • 当您使用http:// 而不是https:// 时有问题吗?您是否在control.authenticate(password='password') 中使用了您的密码?如果您在 tor 上没有密码,那么您是否尝试在没有密码的情况下运行它?
  • 如果你以管理员身份运行它 - sudo python script.py,它可能会在没有Permission denied: '/run/tor/control.authcookie' 的情况下运行 - 但奇怪的是它有访问它的问题。
猜你喜欢
  • 2012-06-13
  • 2022-11-26
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
相关资源
最近更新 更多