[Twisted-Python] Question about writing a server that accepts multiple clients with independent processing

Gabriel Rossetti mailing_lists at evotex.ch
Thu Feb 21 09:06:43 EST 2008

Phil Mayers wrote:
>> Thank you Stephan, ok, but is an instance of a protocol created for 
>> every connection? I tried to add data received  by the protocol to a 
> For TCP connections, assuming everything is coded correctly, yes.
>> list, and if I get multiple connections that list is filled with all 
>> the connection's data, like if only one instance existed.
> You didn't by any chance use a class variable did you e.g.:
> class foo(...):
>   my_data = []
>   def dataReceived(self, data):
>     self.my_data.append(data)
Yes, that must be it :

    class XmlStreamTestSrv(xmlstream.XmlStream):

        def __init__(self):
            self.message = []

even though I though that in python if you put a "self." in front it was 
an instance variable and not a class variable. I have been also having 
some strange problems, like if I overload

    def connectionMade(self):

it never gets called, even though the parent class (XmlStream) also has 
it, and also I can't call self.transport.getPeer() or 
xmlstream.XmlStream.transport.getPeer(), I get :

    print 'Connection from %s!' % 
    exceptions.AttributeError: 'NoneType' object has no attribute 'getPeer'

so maybe something is wrong somewhere.
> ...is wrong. "my_data" is shared by all instances of the class and is 
> mutable. You'll need to create instance variables, usually (I believe) 
> as part of the factory process.
Ok, I'll look into that, thanks.
> If you can write a minimal example showing the problem, someone can 
> probably tell you what you're doing wrong.
Ok, thanks, here's a minimal example :

    from twisted.words.xish import xmlstream, domish
    from twisted.internet import reactor, protocol
    from twisted.internet.protocol import Protocol, Factory
    from twisted.words.xish import xpath

    class XmlStreamTestSrv(xmlstream.XmlStream):

        def __init__(self):
            self.message = []

        def connectionMade(self): # never called, why?
            print 'Connected :' #, string(self.transport.getPeer().host)

        def _onHeader(self, element):
            #print "got header from % : %s" % 
(str(self.transport.getPeer().host), element.toXml())

        def _connected(self, xs):
            #print 'Connection from %s!' % 
            xs.addObserver("/header", self._onHeader)

    if __name__ == "__main__":

        c = XmlStreamTestSrv()

        f = xmlstream.XmlStreamFactory()
        #f.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, c._connected)
        f.addBootstrap(xmlstream.STREAM_START_EVENT, c._connected)

        reactor.listenTCP(4321, f)

Gabriel Rossetti

More information about the Twisted-Python mailing list