Reputation: 155
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}")
Upvotes: 1
Views: 858
Reputation: 169267
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.
Upvotes: 2