Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an object over a TCP server network in python

I am currently working on a small project experimenting with different regions of python. I decided to make a multi-client TCP server in python, and wanted to be able to send a "packet" through the server, it be received by the other clients then parsed. However, I get an error if I try to send the packet, saying I must send either bytes or a string, is there a way to convert the object into bytes, then back or send the packet object through the server itself.

## EDIT ##

I have researched into UDP servers, and I do not believe that is what I am looking for, I probably provided too little information. I am creating a small trial game, for me and some friends to mess about on, I need there to be a constant connection to the server, as information is going to be constantly being sent across the network such as location,direction,speech,inventory etc. and I wanted a way to turn the entire Entity class into a byte array then send that and it be turned back into an instance of the Entity class when it was received.

like image 461
Jack Avatar asked Jul 18 '26 19:07

Jack


1 Answers

You could use pickle to serialize/deserialize objects to strings and back. https://docs.python.org/2/library/pickle.html

like image 142
robertfriberg Avatar answered Jul 20 '26 09:07

robertfriberg