为什么 Signal.NEWNYM
没有生成新的 IP 地址?
from stem import Signal
from stem.control import Controller
import requests
with Controller.from_port(port=9051) as controller:
print('Ready')
controller.authenticate(password='872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C')
print("Success!")
controller.signal(Signal.NEWNYM)
print("New Tor connection processed")
print(requests.get('http://icanhazip.com').content)
最佳答案
NEWNYM 信号并不用于获取新的导出节点,它只是将当前电路标记为脏并确保新连接将使用新电路。
来自 stem docs :
An important thing to note is that a new circuit does not necessarily mean a new IP address. Paths are randomly selected based on heuristics like speed and stability. There are only so many large exits in the Tor network, so it's not uncommon to reuse an exit you have had previously.
Tor does not have a method for cycling your IP address. This is on purpose, and done for a couple reasons. The first is that this capability is usually requested for not-so-nice reasons such as ban evasion or SEO. Second, repeated circuit creation puts a very high load on the Tor network, so please don't!
编辑。我最初错过了您在提出请求时没有使用 Tor(代理)!这是问题的第一部分。
您应该首先安装对 requests
库的 socks
支持 (pip install requests[socks]
),然后通过本地 Tor 代理发出请求,像这样:
requests.get('https://httpbin.org/ip',
proxies={'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}).text
关于python - Stem 不会在 Python 中生成新的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45092345/