Skip to main content
 首页 » 编程设计

python之Stem Controller 的新身份不改变IP

2024年10月01日8shanyou

以下问题看起来很像 SO 上已有的许多相关问题(我已经通读了它们,但我的问题略有不同并且仍然存在)。

我使用 stem 库在 Python 3.6.1 中编写了以下代码(在 macOS Sierra 和 Ubuntu 上均进行了测试)。它所做的只是创建一个新的 tor 进程(配置为使用意大利 ip),打开一个 Controller 连接到它并在尝试获取新的 tor 身份并等待 30 秒后检查 IP。

我的代码只不过是对 stem 库文档稍作修改的版本,因此预计可以正常工作。

所有代码似乎都运行良好,我没有收到任何错误/异常,但每次我获得相同的 IP(有时是第二个 IP,但在两者之间切换)

这是代码(main.py):

import stem.process 
import pycurl 
import io 
import time 
 
from stem.util import term 
from stem.control import Controller 
from stem import Signal 
 
 
TOR_HOST = '127.0.0.1' 
TOR_SOCKS_PORT = 9050 
TOR_CONTROL_PORT = 9051 
TOR_LANG = 'it' 
 
SITE_URL = 'https://www.atagar.com/echo.php' 
 
 
def print_bootstrap_lines(line): 
    if "Bootstrapped " in line: 
        print(term.format(line, term.Color.BLUE)) 
 
 
def query(url): 
    output = io.BytesIO() 
 
    conn = pycurl.Curl() 
    conn.setopt(pycurl.URL, url) 
    conn.setopt(pycurl.PROXY, TOR_HOST) 
    conn.setopt(pycurl.PROXYPORT, TOR_SOCKS_PORT) 
    conn.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME) 
    conn.setopt(pycurl.WRITEFUNCTION, output.write) 
 
    try: 
        conn.perform() 
        return output.getvalue().decode('ascii') 
    except pycurl.error as exc: 
        return "Unable to reach %s (%s)" % (url, exc) 
 
 
tor = stem.process.launch_tor_with_config( 
    config={ 
        'SocksPort': str(TOR_SOCKS_PORT), 
        'ControlPort': str(TOR_CONTROL_PORT), 
        'ExitNodes': '{' + TOR_LANG + '}' 
    }, 
    init_msg_handler=print_bootstrap_lines 
) 
 
ctrl = Controller.from_port(TOR_HOST, port=TOR_CONTROL_PORT) 
ctrl.authenticate() 
 
print(query(SITE_URL)) 
for _ in range(10): 
    ctrl.signal(Signal.NEWNYM) 
    time.sleep(30) 
    print(query(SITE_URL)) 
 
ctrl.close() 
tor.kill() 

程序输出如下:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/tommaso/PycharmProjects/test/main.py 
May 16 16:33:15.000 [notice] Bootstrapped 0%: Starting 
May 16 16:33:16.000 [notice] Bootstrapped 80%: Connecting to the Tor network 
May 16 16:33:17.000 [notice] Bootstrapped 85%: Finishing handshake with first hop 
May 16 16:33:17.000 [notice] Bootstrapped 90%: Establishing a Tor circuit 
May 16 16:33:17.000 [notice] Bootstrapped 100%: Done 
IP Address: 162.220.246.230 (162.220.246.230:45631) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:33604) 
Locale:  
 
IP Address: 5.249.145.164 (torexit-readme.balist.es:42397) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:53925) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:42953) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:60250) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:55945) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:44077) 
Locale:  
 
IP Address: 5.249.145.164 (torexit-readme.balist.es:46375) 
Locale:  
 
IP Address: 162.220.246.230 (162.220.246.230:33205) 
Locale:  
 
IP Address: 5.249.145.164 (torexit-readme.balist.es:47870) 
Locale:  
 
 
Process finished with exit code 0 

您知道为什么我每次都获得相同的 1-2 个 IP,以及如何解决这个问题吗?我不确定这里出了什么问题。 也许只有两个来自意大利的导出节点(具有意大利IP)?

谢谢

请您参考如下方法:

您的代码看起来不错,根据您的评论和我看到的输出,它似乎可以正常工作。

您在该页面上看到的意大利的 64-79 个节点中,只有少数实际上是导出(您需要查看图标标志才能确定)。根据https://atlas.torproject.org/#search/country:it%20flag:Exit 意大利的导出比你想象的要少得多。在撰写本文时,网上似乎有大约 6 个导出。

至少有一个容量非常低 (75 KiB/s),可能无法满足您的请求,而我看到的另一个不允许端口 80 或 443 上的导出流量(因此它不合适,也不会被选中供您使用)。

总而言之,您只获得 2 个 IP 的事实听起来是正确的。这是一个网络容量问题(因为在您想要的国家/地区没有理想数量的导出)而不是代码问题。