[Twisted-web] parsing xml coming from a socket

Antonis Kaklis kaklis at gmail.com
Tue Jun 1 09:46:58 EDT 2010

Hi to all, and since this is my first post, congratulations for the perfect
i have the following code

class SimpleServer(LineReceiver):

    def connectionMade(self):
        print 'Connection from: ', self.transport.client

    def connectionLost(self, reason):
        print self.transport.client, 'Disconnected'

    def dataReceived(self, line):
        """Here the XML Parser"""

        p = xml.parsers.expat.ParserCreate()

        p.StartElementHandler = start_element
        p.EndElementHandler = end_element
        p.CharacterDataHandler = char_data
        p.Parse(line, 1)

I got the following error
--- <exception caught here> ---
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/selectreactor.py", line 146, in
    why = getattr(selectable, method)()
  File "/usr/lib/python2.6/site-packages/Twisted-10.0.0-py2.6-linux-
x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
    return self.protocol.dataReceived(data)
  File "stdiodemo.py", line 419, in dataReceived
    p.Parse(line, 1)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

The XML Message is coming in the form of:

POST /test/pcp/Listener HTTP/1.1
Content-Length: 547

<pttv_control_message version="1.0-M4-SNAPSHOT" build="599"

    <message>OK, found 5 session entries</message>
Please give me some hints.
How to parse the incoming XML and how to ommit the headers from the
beginning of the message?

Thanks in advance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20100601/af08609e/attachment.htm 

More information about the Twisted-web mailing list