Sockets and xml problem

K

kaklis

Hi in the following code

class MyClientHandler(SocketServer.BaseRequestHandler):
def handle(self):
print self.client_address, now( )
time.sleep(5)
while True:
xmltxt = self.request.recv(1024) <--is this ok -
enough?
if not xmltxt: break

doc = minidom.parseString(data)
<--- it also fails for parse(data)
rootNode = doc.documentElement

level = 0

walk(rootNode, outFile, level)
<---just a function to print the xml
self.request.send('Echo=>%s at %s' % (data, now( )))
rootNode = doc.documentElement
level = 0
walk(rootNode, outFile, level)
self.request.send('Echo=>%s at %s' % (data, now( )))
self.request.close( )

# make a threaded server, listen/handle clients forever
myaddr = (myHost, myPort)
server = SocketServer.ThreadingTCPServer(myaddr, MyClientHandler)
server.serve_forever( )


I want to send XML messages from my client. The server sends back the
XML it receives but
the parser exits with error codes.
What am i doing wrong.

Thanks in advance
 
S

Stefan Behnel

(e-mail address removed), 28.05.2010 13:50:
Hi in the following code

class MyClientHandler(SocketServer.BaseRequestHandler):
def handle(self):
print self.client_address, now( )
time.sleep(5)
while True:
xmltxt = self.request.recv(1024)<--is this ok -
enough?

Depends. If your messages are never larger than 1K, this is enough.
Otherwise, you have to collect the data, instead of parsing each chunk
separately.

I suggest using the incremental parser in xml.etree.ElementTree, which
allows you to push more data into the parser as it comes in. When done,
call it's .close() method to retrieve the result.

http://docs.python.org/library/xml.etree.elementtree.html#xmltreebuilder-objects

I want to send XML messages from my client. The server sends back the
XML it receives but the parser exits with error codes.

You should also rethink your approach one more time. Are you sure that a
raw socket is a good protocol for sending your messages? In many cases, a
proper higher-level transport protocol like HTTP is much better suited. If
you provide more details about what you are trying to do, others may be
able to help you further.

Stefan
 
K

kaklis

(e-mail address removed), 28.05.2010 13:50:



Depends. If your messages are never larger than 1K, this is enough.
Otherwise, you have to collect the data, instead of parsing each chunk
separately.

I suggest using the incremental parser in xml.etree.ElementTree, which
allows you to push more data into the parser as it comes in. When done,
call it's .close() method to retrieve the result.

http://docs.python.org/library/xml.etree.elementtree.html#xmltreebuil...


You should also rethink your approach one more time. Are you sure that a
raw socket is a good protocol for sending your messages? In many cases, a
proper higher-level transport protocol like HTTP is much better suited. If
you provide more details about what you are trying to do, others may be
able to help you further.

Stefan

Stefan first of all thank you for your response.
I don't want anything fancy. Just a simple server that accepts xml
messages from multple clients in xml,
parses the XML and show it in a console.
Antonis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top