Skip to main content
 首页 » 编程设计

python之文件中生成的词干列表 Tor 电路

2024年12月31日11lautakyan007

我正在尝试使用 Stem 启动 Tor 连接,然后列出所选的节点。

使用他们网站上的其他问题和常见问题解答,我可以做其中之一,但不能同时做。

例如,我可以启动一个 Tor 电路

  tor_process = stem.process.launch_tor_with_config( 
    config = { 
      'ControlPort': '2778', 
      'Log': [ 
        'NOTICE stdout', 
        'ERR file /tmp/tor_error_log', 
      ], 
    "MaxCircuitDirtiness" : str(60*60*24*30), 
    "NewCircuitPeriod" : str(60*60*24*30), 
    }, 
  ) 

如果我使用 here 中的代码单独启动 Tor,我可以单独查看电路节点.

但是如果我尝试两者都做,例如:

from stem import CircStatus 
from stem.control import Controller 
import stem 
import stem.connection 
import stem.process 
 
from stem.control import Controller 
 
if __name__ == '__main__': 
  tor_process = stem.process.launch_tor_with_config( 
    config = { 
      'ControlPort': '2778', 
      'Log': [ 
        'NOTICE stdout', 
        'ERR file /tmp/tor_error_log', 
      ], 
    "MaxCircuitDirtiness" : str(60*60*24*30), 
    "NewCircuitPeriod" : str(60*60*24*30), 
    }, 
  ) 
 
  with Controller.from_port() as controller: 
    controller.authenticate() 
 
    for circ in controller.get_circuits(): 
      if circ.status != CircStatus.BUILT: 
        continue  # skip circuits that aren't yet usable 
 
      entry_fingerprint = circ.path[0][0] 
      entry_descriptor = controller.get_network_status(entry_fingerprint, None) 
 
      if entry_descriptor: 
        print "Circuit %s starts with %s" % (circ.id, entry_descriptor.address) 
      else: 
        print "Unable to determine the address belonging to circuit %s" % circ.id 
  tor_process.kill() 

我得到一个错误:

Traceback (most recent call last): 
  File "circuit.py", line 22, in <module> 
    with Controller.from_port() as controller: 
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 1024, in from_port 
    control_port = stem.connection._connection_for_default_port(address) 
  File "/usr/local/lib/python2.7/dist-packages/stem/connection.py", line 1058, in _connection_for_default_port 
    raise exc 
stem.SocketError: [Errno 111] Connection refused 

这通常是我尝试在没有单独启动 tor 的情况下执行操作时遇到的错误。

有没有办法结合这两个操作?

编辑:有,我忘了我设置了 ControlPort 在下面回答了我的问题。

请您参考如下方法:

答案很简单,我忘记在config中设置端口了,我设置了ControlPort : 2778,但是打开了Contoller.from_port() 使用默认端口 9051。

使用 Controller.from_port(port = 2778) 解决了这个问题。