简体   繁体   English

Python Tor torrequest/stem 发出来自特定国家/地区的 get 请求

[英]Python Tor torrequest/stem make a get request from a specific country

I'm working on a web crawler to generate some web traffic, figured I'll be needing to use tor (reset_identity()) after I visit the desired website.我正在开发一个网络爬虫来生成一些网络流量,我想在我访问所需的网站后需要使用 tor (reset_identity())。 Also I need my connection to exit through Germany specifically.此外,我需要我的连接才能专门从德国退出。 I've been working with couple of libraries (stem and torrequest) each of which I've hit a dead end...我一直在与几个库(词干和 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)

Works just fine, I get two ip addresses: first is my ip address and the second is the tor exit node.工作得很好,我得到了两个 ip 地址:第一个是我的 ip 地址,第二个是 Tor 出口节点。 But I still need it to exit through Germany, So I used launch_tor_with_config().但是我仍然需要它通过德国退出,所以我使用了launch_tor_with_config()。

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

But from here on I have no idea how to handle the get request.但是从这里开始我不知道如何处理 get 请求。 I tried:我试过:

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)

And it finishes with code 1:它以代码 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

I'm using guest kali linux on windows 10 host with python3.7.我在带有python3.7的Windows 10主机上使用guest kali linux。

Thanks a bunch on advance !!提前致谢!!

After about a day and a half of work I've found the solution.经过大约一天半的工作,我找到了解决方案。 I couldn't proceed with launch_tor_with_config() as hard as I've tried.我无法像我尝试的那样努力继续使用 launch_tor_with_config()。 Instead I've gone for a workaround.相反,我已经寻求解决方法。 Unlike what the web says changing?/adding 'ExitNodes': '{de}' in your tor-browser_en-US/Browser/TorBrowser/Data/Tor/torrc won't help, neither will /etc/tor/torrc.与网络所说的改变不同?/在您的 tor-browser_en-US/Browser/TorBrowser/Data/Tor/torrc 中添加 'ExitNodes': '{de}' 不会有帮助,/etc/tor/torrc 也不会。 What I've done is change the torrequest code, adding 'ExitNodes': '{de}' as follows:我所做的是更改 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)

Best regards.此致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM