通过 Tor 使用 Python 发出请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30286293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make requests using Python over Tor
提问by Sachin Kelkar
I want to make multiple GET requests using Tor to a webpage. I want to use a different ipaddress for each request.
我想使用 Tor 向网页发出多个 GET 请求。我想为每个请求使用不同的 ipaddress。
import socks
import socket
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket
import requests
print (requests.get('http://icanhazip.com')).content
Using this, I made one request. How can I change the ipaddress to make another?
使用这个,我提出了一个要求。如何更改 ipaddress 以创建另一个?
采纳答案by jamescampbell
Here is the code you want to use (download the stem package using pip install stem
)
这是您要使用的代码(使用 下载stem 包pip install stem
)
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password='your password set for tor controller port in torrc')
print("Success!")
controller.signal(Signal.NEWNYM)
print("New Tor connection processed")
Good luck and hopefully that works.
祝你好运,希望能奏效。
回答by shad0w_wa1k3r
There are 2 aspects to your question -
您的问题有两个方面-
- Making requests using Tor
- Renewing the connection as per requirement (in your case, after every request)
- 使用 Tor 发出请求
- 根据要求更新连接(在您的情况下,在每次请求后)
Part 1
第1部分
The first one is easy to do with the latest (upwards of v2.10.0) requests
library with an additional requirement of requests[socks]
for using the socks proxy.
第一个很容易用最新的(v2.10.0 以上)requests
库完成,但需要额外的requests[socks]
使用socks 代理。
Installation-
安装-
pip install requests requests[socks]
Basic usage-
基本用法-
import requests
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
# Make a request through the Tor connection
# IP visible through Tor
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
# Above should print an IP different than your public IP
# Following prints your normal public IP
print(requests.get("http://httpbin.org/ip").text)
Part 2
第2部分
To renew the Tor IP, i.e. to have a fresh visible exit IP, you need to be able to connect to the Tor service through it's ControlPort
and then send a NEWNYM
signal.
要更新 Tor IP,即拥有一个新的可见退出 IP,您需要能够通过它连接到 Tor 服务ControlPort
,然后发送NEWNYM
信号。
Normal Tor installation does not enable the ControlPort
by default. You'll have to edit your torrc fileand uncomment the corresponding lines.
ControlPort
默认情况下,正常 Tor 安装不会启用。您必须编辑您的torrc 文件并取消注释相应的行。
ControlPort 9051
## If you enable the controlport, be sure to enable one of these
## authentication methods, to prevent attackers from accessing it.
HashedControlPassword 16:05834BCEDD478D1060F1D7E2CE98E9C13075E8D3061D702F63BCD674DE
Please note that the HashedControlPassword
above is for the password "password"
. If you want to set a different password, replace the HashedControlPassword
in the torrc by noting the output from tor --hash-password "<new_password>"
where <new_password>
is the password that you want to set.
请注意,HashedControlPassword
以上是密码"password"
。如果你想设置不同的密码,更换HashedControlPassword
时指出,从输出中的torrctor --hash-password "<new_password>"
这里<new_password>
是您要设置的密码。
................................................................................
………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………………………
Warning for Windows users:see post here.
Windows 用户警告:请参阅此处的帖子。
There is an issue on windows where the setting for the controlport in the torrc file is ignored if tor was installed using the following command:
如果使用以下命令安装了 tor,则 Windows 上存在一个问题,其中 torrc 文件中的 controlport 设置将被忽略:
tor --service install
To resolve the issue, after editing your torrc file, type the following commands:
要解决此问题,请在编辑您的 torrc 文件后,键入以下命令:
tor --service remove
tor --service install -options ControlPort 9051
................................................................................
………………………………………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………………………………
Okay, so now that we have Tor configured properly, you will have to restart Tor if it is already running.
好的,现在我们已经正确配置了 Tor,如果 Tor 已经在运行,你必须重新启动它。
sudo service tor restart
Tor should now be up & running on the 9051 ControlPort
through which we can send commands to it. I prefer to use the official stem libraryto control Tor.
Tor 现在应该在 9051 上启动并运行ControlPort
,我们可以通过它向它发送命令。我更喜欢使用官方的stem库来控制Tor。
Installation -
安装 -
pip install stem
You may now renew the Tor IP by calling the following function.
您现在可以通过调用以下函数来更新 Tor IP。
Renew IP-
更新IP-
from stem import Signal
from stem.control import Controller
# signal TOR for a new connection
def renew_connection():
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password="password")
controller.signal(Signal.NEWNYM)
To verify that Tor has a new exit IP, just rerun the code from Part 1. For some reason unknown to me, you need to create a new session
object in order to use the new IP.
要验证 Tor 是否具有新的退出 IP,只需重新运行第 1 部分中的代码。出于某种我不知道的原因,您需要创建一个新session
对象才能使用新 IP。
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
回答by Tobias
The requests
in requesocks
is super old, it doesn't have response.json()
and many other stuff.
该requests
中requesocks
是超级老,它不具备response.json()
和许多其他的东西。
I would like to keep my code clean. However, requests
currently doesn't have socks5 supported yet (for more detail, read this thread https://github.com/kennethreitz/requests/pull/478)
我想保持我的代码干净。但是,requests
目前尚不支持socks5(有关更多详细信息,请阅读此线程https://github.com/kennethreitz/requests/pull/478)
So I used Privoxy
as a http proxy that connects Tor for now.
所以我Privoxy
暂时用作连接 Tor 的 http 代理。
Install and configure Privoxy on Mac
在 Mac 上安装和配置 Privoxy
brew install privoxy
vim /usr/local/etc/privoxy/config
# put this line in the config
forward-socks5 / localhost:9050 .
privoxy /usr/local/etc/privoxy/config
Install and configure Privoxy on Ubuntu
在 Ubuntu 上安装和配置 Privoxy
sudo apt-get install privoxy
sudo vim /etc/privoxy/config
# put this line in the config
forward-socks5 / localhost:9050 .
sudo /etc/init.d/privoxy restart
Now I can use Tor like a http proxy. Below is my python script.
现在我可以像使用 http 代理一样使用 Tor。下面是我的python脚本。
import requests
proxies = {
'http': 'http://127.0.0.1:8118',
}
print requests.get('http://httpbin.org/ip', proxies=proxies).text
回答by Karimov Dmitriy
Requests supports proxiesusing the SOCKS protocol from version 2.10.0.
从 2.10.0 版开始,请求支持使用 SOCKS 协议的代理。
import requests
proxies = {
'http': 'socks5://localhost:9050',
'https': 'socks5://localhost:9050'
}
url = 'http://httpbin.org/ip'
print(requests.get(url, proxies=proxies).text)
回答by Erdi Aker
You can use torrequest
library (shameless plug). It's available on PyPI.
您可以使用torrequest
库(无耻的插件)。它在 PyPI 上可用。
from torrequest import TorRequest
with TorRequest() as tr:
response = tr.get('http://ipecho.net/plain')
print(response.text) # not your IP address
tr.reset_identity()
response = tr.get('http://ipecho.net/plain')
print(response.text) # another IP address, not yours
回答by JinSnow
This answer complete the one of Ashish Nitin Patil for windows(feel free to update this answer)
此答案完成了适用于Windows的 Ashish Nitin Patil 之一 (随时更新此答案)
Part 2
第2部分
ControlPort 9051
## If you enable the controlport, be sure to enable one of these
## authentication methods, to prevent attackers from accessing it.
HashedControlPassword 16:05834BCEDD478D1060F1D7E2CE98E9C13075E8D3061D702F63BCD674DE
The HashedControlPassword
above is the password. If you want to set a different password in the console navigate to \Tor Browser\Browser\TorBrowser\Tor
and type the following commands: tor.exe --hash-password password_XYZ | more
). It will give you something like HashedControlPassword 16:54C092A8...
This is your password. Now you can add it to the torrc file (Tor Browser\Browser\TorBrowser\Data\Tor\torrc
).
在HashedControlPassword
上面的密码。如果要在控制台中设置不同的密码,请导航至\Tor Browser\Browser\TorBrowser\Tor
并键入以下命令:) tor.exe --hash-password password_XYZ | more
。它会给你类似HashedControlPassword 16:54C092A8...
这是你的密码的东西。现在您可以将其添加到 torrc 文件 ( Tor Browser\Browser\TorBrowser\Data\Tor\torrc
) 中。
You will need then to restart Tor:
然后你需要重新启动 Tor:
tor --service remove
tor --service install -options ControlPort 9051
To check if that works type netstat -an
you will now see that port 9051 is open.
要检查该类型netstat -an
是否有效,您现在将看到端口 9051 已打开。
Notice that tor --service install -...
will create Tor Win32 Service
. For some reason, it seems you have to stop the service to use the browser(run services.msc
)
请注意,这tor --service install -...
将创建Tor Win32 Service
. 出于某种原因,您似乎必须停止该服务才能使用浏览器(运行services.msc
)
EDIT:you will find many pieces of information here(About port number & proxy, Tor, Privoxy, Auto switch user agent...).
编辑:你会在这里找到很多信息(关于端口号和代理、Tor、Privoxy、自动切换用户代理......)。
回答by N.G.Searching
This code works fine. Using Tor, it changes the IP address after each request.
这段代码工作正常。使用 Tor,它会在每次请求后更改 IP 地址。
import time, socks, socket
from urllib2 import urlopen
from stem import Signal
from stem.control import Controller
nbrOfIpAddresses=3
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password = 'my_pwd')
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
for i in range(0, nbrOfIpAddresses):
newIP=urlopen("http://icanhazip.com").read()
print("NewIP Address: %s" % newIP)
controller.signal(Signal.NEWNYM)
if controller.is_newnym_available() == False:
print("Waitting time for Tor to change IP: "+ str(controller.get_newnym_wait()) +" seconds")
time.sleep(controller.get_newnym_wait())
controller.close()
回答by James Brown
You can try pure-python tor protocol implementation Torpy. No need original Tor client or Stem dependency at all.
您可以尝试纯 python Tor 协议实现Torpy。根本不需要原始 Tor 客户端或 Stem 依赖项。
$ pip3 install torpy[requests]
...
$ python3.7
>>> from torpy.http.requests import TorRequests
>>> with TorRequests() as tor_requests:
... print("build circuit")
... with tor_requests.get_session() as sess:
... print(sess.get("http://httpbin.org/ip").json())
... print(sess.get("http://httpbin.org/ip").json())
... print("renew circuit")
... with tor_requests.get_session() as sess:
... print(sess.get("http://httpbin.org/ip").json())
... print(sess.get("http://httpbin.org/ip").json())
...
build circuit
{'origin': '23.129.64.190, 23.129.64.190'}
{'origin': '23.129.64.190, 23.129.64.190'}
renew circuit
{'origin': '198.98.50.112, 198.98.50.112'}
{'origin': '198.98.50.112, 198.98.50.112'}
So each time when you getting new session you get new identity (basically you get new circuit with new exit node). See more examples at readme file https://github.com/torpyorg/torpy
因此,每次当您获得新会话时,您都会获得新身份(基本上,您将获得带有新出口节点的新电路)。在自述文件https://github.com/torpyorg/torpy 中查看更多示例