[Twisted-Python] Noob Question

Shelby Ramsey sicfslist at gmail.com
Sun Mar 15 09:55:25 EDT 2009


Peter,

Thanks for the assistance.  I think you and David have me on the right
path.  Just to clarify the protocol looks like this:

Content-Length: 984
Content-Type: text/event-xml

<event>
  <headers>
   $bunchofinfo ...
  </headers>
</event>

Where the body (and the content length) start and stop with the <event> ...
</event>.

Here is what I've done so far:

from twisted.internet import reactor, protocol
from twisted.protocols import basic
from re import compile, findall

bl_p = compile('Content-Length:\s(\d+)')

class My_Client(basic.LineReceiver):
    def __init__(self):
        self.body_length = None

    def connectionMade(self):
        self.transport.write("login \r\n\r\n")
        self.transport.write("sendevents\r\n\r\n")

    def lineReceived(self, line):
        print line
        bl_m = bl_p.findall(line)
        if bl_m:
            print 'Match:'
            self.body_length = int(bl_m[0]) - len(line) # this doesn't
really work because it doesn't factor in the len of the next line
            self.setRawMode()

    def rawDataReceived(self, data):
       datalen = len(data)
       if self.body_length > datalen:
           self.body_length -= datalen
           print data
       else:
           part = data[:self.body_length]
           extra = data[self.body_length:]
           print 'Left Over\n:'
           print extra
           self.setLineMode(extra=extra)

    def connectionLost(self, reason):
        print "%s" % (reason)

class My_Factory(protocol.ClientFactory):
    protocol = My_Client

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()

def main():
    f = FS_Factory()
    reactor.connectTCP("$IP", $PORT, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

To me ... this looks somewhat correct ... but it doesn't seem to actually
ever use the line received method (note the print statement ...).  For
instance upon logging in it should respond with a +OK Logged In ... but it
never prints that.

And then when it receives an "<event>..." it actually dies with this error:

[Failure instance: Traceback (failure with no frames): <class
'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.
]
Connection lost - goodbye!

So I'm a bit lost as to what the impact of adding the rawDataReceived method
has done.

Thanks!

SDR
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090315/3f86d08b/attachment.htm 


More information about the Twisted-Python mailing list