python扭曲:对每个ID强制执行单个连接 [英] python twisted: enforcing a single connection per id

查看:89
本文介绍了python扭曲:对每个ID强制执行单个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台扭曲的服务器,该服务器使用SSL套接字并使用证书来标识连接到该服务器的不同客户端.我想强制执行一种状态,其中每个可能的ID只有一个连接.我可以想到的两种方法是跟踪已连接的ID,然后不允许使用相同的ID进行第二个连接,或者不允许第二个连接并立即终止第一个连接.我想稍后再尝试,但是遇到了一些问题(我将在最后解释我的选择)

I have a twisted server using SSL sockets and using certificates to identify the different clients that connect to the server. I'd like to enforce the state where there is only one connection by each possible id. The two ways I can think of is to keep track of connected ids and then not allow a second connection by the same id or allow the second connection and immediately terminate the first. I'm trying to do the later but am having some issues (I'll explain my choice at the end)

我在工厂类中存储一个连接列表,然后在SSL握手后,将客户端的ID与该列表进行比较.如果它已经在该列表中,我尝试在其上调用.transport.abortConnection().然后,我想做一些普通的事情来在数据库中记录新的连接.但是,对abortConnection()的调用似乎并没有直接调用connectionLost(),这是我进行清理并调用数据库以说连接丢失的地方.因此,我的代码随后记录了该ID已连接,但随后调用connectionLost()导致数据库似乎断开了该ID.

I'm storing a list of connections in the factory class and then after the SSL handshake I compare the client's id with that list. If it's already in that list I try to call .transport.abortConnection() on it. I then want to do the normal things I do to record the new connection in my database. However, the call to abortConnection() doesn't seem to call connectionLost() directly which is where I do my cleanup and calls to the database to say that a connection was lost. So, my code then records that the id connected but later a call is made to connectionLost() resulting in the database appearing to have that id disconnected.

是否有某种方法可以阻止传入的第二个连接进行进一步处理,直到第一个连接完成对断开连接的处理?

选择说明:这样做的全部原因是,我有NAT背后的客户端,这些客户端似乎在相当定期地(每1-3天一次)更改其IP地址.连接的设备只是断开了它们的连接,然后尝试与新IP重新连接.但是,我的服务器未收到有关断开连接的通知,通常必须使连接超时.但是,在服务器使连接超时之前,客户端有时会设法重新连接,然后服务器处于同一客户端具有两个明显连接的状态.因此,通常第一个连接是我真正想要终止的连接.

Choice explanation: The whole reason I'm doing this is I have clients behind NATs that appear to be changing their IP address on a fairly regular basis (once a every 1-3 days). The devices connecting will just have their connections uncleanly severed and then they try to reconnect with the new IP. However, my server isn't notified about the disconnect and usually has to timeout the connection. Before the server times out the connection, though, the client sometimes manages to reconnect and the server then is in a state with two apparent connections by the same client. So, typically the first connection is the one I really want to terminate.

推荐答案

一旦确定了连接的ID,就可以在新"连接的传输中调用self.transport.pauseProducing(),这将阻止任何通知,直到您调用self.transport.resumeProducing().如果存在新连接,则可以从oldConnection.connectionLost()呼叫newConnection.transport.resumeProducing().

Once you have determined the ID of the connection, you can call self.transport.pauseProducing() on the "new" connection's transport, which will prevent any notifications until you call self.transport.resumeProducing(). You can then call newConnection.transport.resumeProducing() from oldConnection.connectionLost(), if a new connection exists.

这篇关于python扭曲:对每个ID强制执行单个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆