我想使用Tor向一个网页发出多个GET请求。我想对每个请求使用不同的IP地址,所以我编写了这个小程序
from stem import Signal
from stem.control import Controller
import requests
def change_ip():
with Controller.from_port(port=9051) as contr:
contr.authenticate(password='abhishek')
contr.signal(Signal.NEWNYM)
session=requests.session()
session.proxies={}
session.proxies['http']='socks5://127.0.0.1:9051'
session.proxies['https']='socks5://127.0.0.1:9051'
for i in range(5):
r=session.get('http://httpbin.org/ip')
print(r.text)
change_ip()
使用这个,我发出了多个请求,但是这个程序没有显示任何输出,它就像我在这张图中显示的一样this is the screenshot of terminal where i run this program and it stucked
但是,当我删除代码的session.proxy区域时,代码正在运行并显示输出,但这对我来说毫无意义,因为我希望在每次请求后更改ip地址
Tor在端口
9050
上运行代理,而不是9051
。端口9051
仅用于控制/更改Tor我还需要在发送singnal后几秒钟才能获得新的IP
当我不为所有URL使用一个会话,而只对普通请求使用一个会话时,效果会更好
在一个会话中,它有时为https://httpbin.org/ip和https://api.ipify.org提供相同的IP,但不为https://icanhazip.com提供相同的IP
如果我在每个循环中创建新会话,它将正常工作
无会话版本
会话版本-每个循环中都有新会话
相关问题 更多 >
编程相关推荐