python - 创建 10 个 Tor 电路实例的有效方法

标签 python sockets ssl connection

我想做的是创建一种快速方法,我可以构建 10 个电路,跳数将始终相同,但我需要它们有一个新的 socks 连接。 (Tor 中的电路就像一个独特的连接)。

我的标准代码是:

circ_to_ip= TorCircuit(ssl_sock)
create_circuits(circ_to_ip, hops_in_circ)

这会创建一个名为 circ_to_ip 的电路,然后我可以通过它创建一个电路,circ_to_ip 会创建一个新的 socks 连接吗?

我尝试了下面的代码:

n = 1
while n < 10:

    n= TorCircuit(ssl_sock)
    create_circuits(n, hops_in_circ)
    print "circuit",n 
    n = n + 1 
sys.exit(0)

然而,当 n 被分配 TorCircuit 时,它变成了一个实例而不是 int,因此失败。

如何创建 10 个电路,我最初的计划只是创建 10 个不同的名称来执行此操作,但这不仅行不通,我还觉得有更好的方法?

如果需要,TorCircuit 类在下面

TOR_CIRCID_COUNTER = 1
class TorCircuit():
    def __init__(self, sock):
        global TOR_CIRCID_COUNTER
        self.hops = []
        self.circId = TOR_CIRCID_COUNTER
        TOR_CIRCID_COUNTER+=1
        self.socket = sock
        self.tempX = 0
        self.packetSendCount = 0
        self.cookie = []

谢谢:)

最佳答案

您的问题是您目前正在用新电路覆盖循环计数器 (n)。

您要做的是将这些电路存储在 list 中.此外,您可能希望将 for 循环与 range 一起使用而不是 while 循环:

storage = [] # Empty list to store all the circuits
for n in range(10):
    circuit = TorCircuit(ssl_sock)
    create_circuits(circuit, hops_in_circ)
    print "circuit", circuit
    storage.append(circuit) # Add the circuit to the list

关于python - 创建 10 个 Tor 电路实例的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25252428/

相关文章:

python - 我可以像在 R 中一样在 python 中运行行吗?

sockets - Windows IOCP-单路应用程序有什么优势?

ruby-on-rails - 在设备上设置 ssl

java - 从 LoZ Oracle 游戏中创建 secret 系统

python - Pandas 用另一个系列中的相应值替换特定单元格

java - tcp 中的 IOUtils.copy()

Java在主线程中监听套接字时读取线程中的控制台输入

ssl - 使用 Phoenix/Elixir 和 nginx 在服务器上设置 https

ssl - 正确实现 OpenSSL RSA 引擎 : questions about rsa_meth_st

python - 最快/最有效的文本和 float 数据存储格式