[Twisted-Python] Closing connections properly
Simon Pickles
sipickles at hotmail.com
Sat Jan 19 08:41:40 EST 2008
Hi,
Having a simple server based on a LineReceiver, what is the proper way
to close connections and delete the class containing those connections
(the class built by BuildProtocol()).
Given the simple code below, Protocol.BuildProtocol is called when I
client connections, and builds and instance of the class Connection.
When the client disconnects, Connection.connectionLost is called, but
the instance continues (__del__ is never called). I even naively tried
added a 'del self' statement to the connectionLost method, but its not
c++ 'delete this'!
How should I delete the closed connection? Is there something in twisted
I can overload, or should I use the server to track connections better
and perform my deleting there?
Thanks
Simon
Example
--------------
class Connection(basic.LineReceiver):
def __init__(self):
self.ipAddress = "000.000.000.000"
self.port = 0
logger.CON("Opening new connection")
def __del__(self): # never called
logger.CON("Closed connection to %s:%d" % (self.ipAddress,
self.port))
def connectionMade(self):
""" Overrides method in basic.LineReceiver """
self.ipAddress = self.transport.getPeer().host
self.port = self.transport.getPeer().port
logger.CON("Log in attempt from %s:%d" % (self.ipAddress,
self.port))
def connectionLost(self, reason):
""" Overrides method in basic.LineReceiver """
logger.CON("%s:%d has closed the connection, %s" %
(self.ipAddress, self.port, reason))
self.transport.loseConnection()
def dataReceived(self, data):
""" Overrides method in basic.LineReceiver """
logger.CON("%s:%d received %s" % (self.ipAddress, self.port, data))
class Server(Protocol):
factory = Factory()
factory.protocol = Connection
def __init__(self, maxClients):
self.max_clients = maxClients
logger.NET("Server started")
def doStart(self):
logger.NET("Port: %d" % globalVars.zoneAddress[1])
logger.NET("...... waiting for a connection ......")
def doStop(self):
logger.NET("Server is now closed")
def buildProtocol(self, addr):
logger.NET("Connection attempt....")
return Connection()
--
Linux user #458601 - http://counter.li.org.
More information about the Twisted-Python
mailing list