1

I've been trying to send an xml string through sockets from a client to a server. The idea is to parse this xml with ElementTree and then use the obtained element.

I can successfully pass the string from client to server, but regardless of how i build the xml in the client ( via file, triple quotes string or using the method within the ElementTree packages), in the server i keep getting the error

xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

I have validated the file/string and I'm not sure where's the mistake since i really don't have any experience in directly using XML files.

This is the XML string

RemoteFunction = '''<?xml version="1.0"?>
<methodCall>
    <methodName>somemethodname</methodName>
    <params>
      <param>
         <value><string>somevalue</string></value>
      </param>
      <param>
         <value><i4>1</i4></value>
      </param>
      <param>
         <value><string>anothervalue</string></value>
      </param>
    </params>
</methodCall>'''

And this is the connection handler function in server

import xml.etree.ElementTree as ET

def client_handler( connection, address):
    connected = True
    while connected:
        msgLen = connection.recv(64).decode("utf-8")
        if msgLen:
            msgLen = int(msgLen)
            msg = connection.recv(msgLen).decode("utf-8")
            if msg == "!Disconnect":
                connected = False
            
            tree = ET.fromstring(msg)

Note: I am aware there's a library for XMLRPC in Python3 but I'm not allowed to use it.

3
  • What do you get when you print msg before trying to parse it as XML? Commented Oct 5, 2020 at 18:43
  • This is the printed msg <?xml version="1.0"?> <methodCall> <methodName>somemethodname</methodName> <params> <param> <value><string>somevalue</string></value> </param> <param> <value><i4>1</i4></value> </param> <param> <value><string>anothervalue</string></value> </param> </params> </methodCall> Commented Oct 5, 2020 at 19:57
  • Your msg is valid xml which can be read fine by both ET and lxml. Commented Oct 5, 2020 at 20:29

2 Answers 2

2

Try this.

from simplified_scrapy import SimplifiedDoc
def client_handler( connection, address):
    connected = True
    while connected:
        msgLen = connection.recv(64).decode("utf-8")
        if msgLen:
            msgLen = int(msgLen)
            msg = connection.recv(msgLen).decode("utf-8")
            if msg == "!Disconnect":
                connected = False

            doc = SimplifiedDoc(msg)
            print (doc.methodName.text, doc.selects('param>text()'))
Sign up to request clarification or add additional context in comments.

Comments

0

This isn't an answer as much as a mistake because i did not notice it was actually working from the start.

The problem was the client was sending 2 message

send(xmldata)
send(DisconnectMsg)

Even if it ElementTree parsed correctly the first message an error would pop out for the second message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.