1

Is there any method for Socket of Python to deliver XML instead of bytes. As the server side only recieves raw XML. I tried the code below but it raises attribute error, it asks for bytes instead of string. Any idea?

  import socket

  server_ip = "192.168.88.52"
  port = 2605

  socket_obj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  socket_obj.settimeout(5.0)

  try:
      socket_obj.connect((server_ip, port, ))
      command = '<SERVICE ID="TA"><TRACKID_GET></TRACKID_GET></SERVICE>'

      socket_obj.send(command)
      print(f"Successfully send the command")

      data = socket_obj.recv(4096)
      print(f"Received Response: {data}")
  except socket.timeout as e:
      print(f"Response TimeOut: {e}")
      socket_obj.close()
  except Exception as e:
      print(f"Failed to connect to the P1 server: {e}")
1
  • A socket is an object that can only send bytes. If you want to send something else than bytes, then you're at least one level of abstraction higher than sockets. But if you really need to send raw XML to a host, then you must find out somehow how that host decodes the bytes it receives. Commented Dec 10, 2019 at 9:07

1 Answer 1

2

There is no such thing as "raw XML" (except as an abstract concept).

There is also no such thing as sending "text" over the network (or writing it into a file for that matter).

Whatever you want to send through your socket will have to be encoded into bytes using some encoding. Just .encode() will default to UTF-8, which will probably do fine here (until it probably won't).

Also, you'll probably want .sendall() to ensure everything has been sent.

Sign up to request clarification or add additional context in comments.

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.