我想使用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
使用这个,我提出了一个请求。如何更改ipaddress以制作另一个?
答案 0 :(得分:26)
以下是您要使用的代码(使用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")
祝你好运,希望有效。
答案 1 :(得分:13)
您可以使用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
答案 2 :(得分:6)
Requests supports proxies使用版本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)
答案 3 :(得分:3)
这个答案完成了 windows 的Ashish Nitin Patil之一 (随意更新此答案)
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
上面的HashedControlPassword
是密码。如果要在控制台中设置其他密码,请导航至\Tor Browser\Browser\TorBrowser\Tor
并键入以下命令:tor.exe --hash-password password_XYZ | more
)。它会给你HashedControlPassword 16:54C092A8...
这样的密码。现在您可以将它添加到torrc文件(Tor Browser\Browser\TorBrowser\Data\Tor\torrc
)。
然后你需要重启Tor:
tor --service remove
tor --service install -options ControlPort 9051
要检查是否有效netstat -an
,您现在会看到端口9051已打开。
请注意tor --service install -...
将创建Tor Win32 Service
。出于某种原因,似乎您必须停止使用浏览器的服务(运行services.msc
)
编辑:您会找到许多信息here(关于端口号和代理,Tor,Privoxy,自动切换用户代理......)。
答案 4 :(得分:3)
此代码工作正常。使用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()
答案 5 :(得分:2)
您可以尝试使用纯Python Tor协议实现Torpy。完全不需要原始的Tor客户端或Stem依赖项。
$ pip3 install torpy
...
$ 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'}
因此,每次获得新会话时,您都会获得新身份(基本上,您将获得具有新出口节点的新电路)。请参阅自述文件https://github.com/torpyorg/torpy
中的更多示例答案 6 :(得分:1)
requests
中的requesocks
超级旧,它没有response.json()
和许多其他内容。
我想保持我的代码干净。但是,requests
目前还没有支持socks5(有关详细信息,请阅读此帖子https://github.com/kennethreitz/requests/pull/478)
所以我使用Privoxy
作为连接Tor的http代理。
在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
在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
现在我可以像使用http代理一样使用Tor。下面是我的python脚本。
import requests
proxies = {
'http': 'http://127.0.0.1:8118',
}
print requests.get('http://httpbin.org/ip', proxies=proxies).text
答案 7 :(得分:1)
一项很好的功能,可以续订您的IP。 Windows示例
def renew_tor_ip():
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password="aAjkaI19!!laksjd")
controller.signal(Signal.NEWNYM)
用法示例
import requests
import time
from stem import Signal
from stem.control import Controller
def get_current_ip():
session = requests.session()
# TO Request URL with SOCKS over TOR
session.proxies = {}
session.proxies['http']='socks5h://localhost:9150'
session.proxies['https']='socks5h://localhost:9150'
try:
r = session.get('http://httpbin.org/ip')
except Exception as e:
print(str(e))
else:
return r.text
#16:8EE7AEE3F32EEEEB605C6AA6C47B47808CA6A81FA0D76546ADC05F0F15 to aAjkaI19!!laksjd
#cmd shell "C:\Users\Arthur\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe" --hash-password aAjkaI19!!laksjd | more
#Torcc config
#ControlPort 9051
#HashedControlPassword 16:8EE7AEE3F32EEEEB605C6AA6C47B47808CA6A81FA0D76546ADC05F0F15
def renew_tor_ip():
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password="aAjkaI19!!laksjd")
controller.signal(Signal.NEWNYM)
for i in range(5):
print(get_current_ip())
renew_tor_ip()
time.sleep(5)