0

我正在开发一个网络爬虫来生成一些网络流量,我想在访问所需的网站后我需要使用 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。

提前感谢一堆!

4

1 回答 1

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)

此致。

于 2020-01-31T22:29:33.223 回答