Reputation:
I'm trying to send a custom object via sockets in Python. My object is defined as:
packet = Packet.Packet(MAGIC_NUMBER,0,sender_next,packet_size,local_buffer)
port_out.send(packet)
But I am getting an error of: builtins.TypeError: a bytes-like object is required, not 'Packet'
I've seen previous posts about using .encode() for str types but how do I do it for a custom object like this? Do I need to make a encode method?
Upvotes: 1
Views: 434
Reputation: 111
You can use pickle
import pickle
packet = Packet.Packet(MAGIC_NUMBER,0,sender_next,packet_size,local_buffer)
port_out.send(pickle.dumps(packet))
https://pymotw.com/3/pickle/index.html
Upvotes: 1