Sockets and xml problem

Discussion in 'Python' started by kaklis@gmail.com, May 28, 2010.

  1. Guest

    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
     
    , May 28, 2010
    #1
    1. Advertisements

  2. , 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
     
    Stefan Behnel, May 28, 2010
    #2
    1. Advertisements

  3. Guest

    On May 28, 3:23 pm, Stefan Behnel <> wrote:
    > , 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#xmltreebuil...
    >
    > > 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


    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
     
    , May 28, 2010
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.