【问题标题】:Python Tor torrequest/stem make a get request from a specific countryPython Tor torrequest/stem 从特定国家/地区发出获取请求
【发布时间】:2020-01-31 15:12:26
【问题描述】:

我正在开发一个网络爬虫来生成一些网络流量,我想在访问所需的网站后我需要使用 tor (reset_identity())。我还需要我的连接才能专门通过德国退出。我一直在使用几个库(stem 和 torrequest),每个库都遇到了死胡同……

import torrequest
import requests

with torrequest.TorRequest(password=None) as tr:
    response = requests.get('http://ipecho.net/plain')
    print("My Original IP Address:", response.text)

    tr.reset_identity()  # Reset Tor
    response = tr.get('http://ipecho.net/plain')
    print("New Ip Address", response.text)

工作得很好,我得到了两个 ip 地址:第一个是我的 ip 地址,第二个是 tor 出口节点。 但是我仍然需要它通过德国退出,所以我使用了launch_tor_with_config()。

torrequest.launch_tor_with_config(
    tor_cmd='/usr/bin/tor',
    config={
        'ExitNodes': '{DE}'  # exiting through Germany
    }
)

但是从这里开始,我不知道如何处理获取请求。我试过了:

import requests
import stem.process

tor_process = stem.process.launch_tor_with_config(tor_cmd='/usr/bin/tor',
                                                  config={
                                                      'ExitNodes': '{DE}'  # exiting through Germany
                                                  }
                                                  )

response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
print("New Ip Address", response.text)

它以代码 1 结束:

Traceback (most recent call last):
  File "/root/PycharmProjects/webtraffic/webtraffic1.py", line 10, in <module>
    response = tor_process.communicate(requests.get('http://ipecho.net/plain'))
  File "/usr/lib/python3.7/subprocess.py", line 964, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.7/subprocess.py", line 1667, in _communicate
    self.stdin.flush()
ValueError: flush of closed file

Process finished with exit code 1

我在带有 python3.7 的 windows 10 主机上使用 guest kali linux。

非常感谢提前!!

【问题讨论】:

    标签: python tor stem


    【解决方案1】:

    经过大约一天半的工作,我找到了解决方案。我无法像我尝试过的那样努力使用 launch_tor_with_config() 。相反,我已经寻求解决方法。与网络上所说的改变不同?/在您的 tor-browser_en-US/Browser/TorBrowser/Data/Tor/torrc 中添加 'ExitNodes': '{de}' 将无济于事,/etc/tor/torrc 也无济于事。 我所做的是更改 torrequest 代码,添加 'ExitNodes': '{de}' 如下:

      def _launch_tor(self):
        return launch_tor_with_config(
          config={
            'SocksPort': str(self.proxy_port),
            'ControlPort': str(self.ctrl_port),
            'ExitNodes': '{de}'
          },
          take_ownership=True)
    

    最好的问候。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2012-01-11
      • 2022-07-09
      • 2020-01-11
      • 1970-01-01
      相关资源
      最近更新 更多