How to get the client IP address after got connection lost in twisted

こ雲淡風輕ζ 提交于 2019-11-29 22:52:10

问题


I know we can get the client (host) IP after connection has been established because at that time we will have transport attribute:

self.transport.getPeer()

but how do I get the IP address of the client in twisted TCP server when it lost connection to the server, as in after it got disconnected.


回答1:


Its a little late for that. I suggest you save this information when you have it. For example:

class YourProtocol(protocol.Protocol):

    def connectionMade(self):
        self._peer = self.transport.getPeer()

    def connectionLost(self):
        print 'Lost connection from', self._peer



回答2:


While this has already been answered, i thought i would quickly add mine so i do not forget this in the future... As we know, the docs for Twisted are .. well twisted...

def connectionLost(self):
    ip, port = self.transport.client
    print ip
    print port

By using the above, you can then simply match the ip/port against what ever database or means you have of keeping track of clients.

I ended up finding by using print vars(self.transport) and seeing the client object in the output/console... using classic php debugging here



来源:https://stackoverflow.com/questions/14278555/how-to-get-the-client-ip-address-after-got-connection-lost-in-twisted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!