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. Advertising

  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. Advertising

  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. Advertising

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.
Similar Threads
  1. Tero Saarni
    Replies:
    2
    Views:
    279
    Tero Saarni
    Aug 7, 2003
  2. Gonçalo Rodrigues

    Help needed in problem with Threads and sockets.

    Gonçalo Rodrigues, Dec 29, 2003, in forum: Python
    Replies:
    0
    Views:
    265
    Gonçalo Rodrigues
    Dec 29, 2003
  3. Jose Alberto Reguero

    Problem with sockets and python 2.5

    Jose Alberto Reguero, Mar 19, 2007, in forum: Python
    Replies:
    0
    Views:
    233
    Jose Alberto Reguero
    Mar 19, 2007
  4. A K

    xml + sockets in java

    A K, Mar 15, 2008, in forum: Java
    Replies:
    4
    Views:
    1,441
    Roedy Green
    Mar 15, 2008
  5. Erik Wasser
    Replies:
    5
    Views:
    428
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page