Hi to all, and since this is my first post, congratulations for the perfect framework. i have the following code
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 _doReadOrWrite 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 user-agent:hjahs Host:127.0.0.1 Content-Length: 547
<pttv_control_message version="1.0-M4-SNAPSHOT" build="599" xmlns="http://1270.0.01/pttvhttp://www.google.com/url?sa=D&q=http://1270.0.01/pttv&usg=AFQjCNEPqmRku5kV0nsb3RICWIxQ8vOV9A">
<cmdReply> <code>200</code> <message>OK, found 5 session entries</message> <sessionList> <session> <id>06d4d59bfdfe10139dd874</id> <subscriberId>82</subscriberId> <deviceClass>and</deviceClass> </session> </sessionList> </cmdReply> </pttv_control_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
On Jun 1, 2010, at 9:46 AM, Antonis Kaklis wrote:
Hi to all, and since this is my first post, congratulations for the perfect framework.
Thank you :).
i have the following code
This code has two major, obvious problems:
def dataReceived(self, line):
Here you say "dataReceived" receives a "line" parameter. But dataReceived does not receive lines, it receives *data*, i.e. arbitrary chunks of bytes. What you want to do is write a *line*Received method; you are already using LineReceiver, so I assume that's what you were trying to do.
If you fix that, the second problem is that you are creating a new XML parser for every line received. Is that actually what you want to do? Your message has some headers, which you will want to skip, and then the body of the message spans multiple lines.
Your "XML message" looks a lot like an HTTP POST (I am assuming the lack of spaces after the headers in your example is a typo). Perhaps you would be better off using the built-in HTTP server in twisted.web, rather than trying to implement your own wire-level protocol?