How to get client IP after connection lost in twisted

I know that we can get the IP address of the client (host) after the connection is established, because at that time we will have the transport attribute:

self.transport.getPeer() 

but how to get the IP address of a client on a twisted TCP server when it lost connection with the server, just like after it disconnected.

+4
source share
2 answers

A bit late for that. I suggest that you keep this information when you have it. For instance:

 class YourProtocol(protocol.Protocol): def connectionMade(self): self._peer = self.transport.getPeer() def connectionLost(self): print 'Lost connection from', self._peer 
+10
source

While this was already answered, I thought I would add my own quickly, so I will not forget about it in the future ... As you know, the documents for Twisted ... are well twisted ...

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

Using the above, you can simply map ip / port to some kind of database or keep in mind client tracking.

I ended the search with print vars(self.transport) and saw the client object in the output / console ... using the classic php debugging here

+2
source

All Articles