3

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.

5
  • It's not clear what you mean by a "packet". Can you explain how it is that your code has a packet that it's trying to send? Commented Feb 16, 2015 at 13:12
  • 1
    Remember that TCP is a reliable stream of data. It has no inherent message boundaries. You need to implement some sort of delineation between messages yourself. Commented Feb 16, 2015 at 13:22
  • @DavidSchwartz I have created a class which stores a reference to what the object is, and then the object itself. Commented Feb 16, 2015 at 13:24
  • @JonathonReinhart I understand that, I was just wondering whether there was a way to decode what I sent back into an object form as if it had lots of variables, then it would take a while to put all that back into the object Commented Feb 16, 2015 at 13:26
  • 1
    Before you write any code, specify the protocol you are going to use to exchange data. This should be done at the byte level. Then you can use any method you want to generate and process the stream of bytes specified. Commented Feb 16, 2015 at 13:30

2 Answers 2

2

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

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

9 Comments

No. Pickle is unsafe, sending pickled objects over the network and unpickling them on the other side is an extremely bad idea and practice.
@JackRadforth Read the warning near the top of the page. "Never unpickle data received from an untrusted or unauthenticated source."
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
@mata: It's not that pickle is unsafe, it's that it's unsafe when used with an untrusted source or if unauthenticated. I send pickled objects over RSA encrypted connections through ssh tunnels to do large scale distributed computing and pickle in that case is not unsafe at all.
@mata: Agreed... that this is bad practice except between two trusted sources. With untrusted/insecure sources you might as well just give away root.
|
1

The simplest possible approach would be to send (gzipped?) JSON'd or msgpack'd objects.

For example, using UDP, this could look something like the below code; note that you would want to reuse the socket object rather than instantiating a new one every time.

import socket
import msgpack

def send_object(obj=None, ip, port):
    if obj:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(msgpack.dumps(obj), (ip, port))

5 Comments

Opening a new socket every time you want to send an object is a bad idea.
Of course, the function is simply there for reference. I'll amend and add a caveat.
The problem is, people copy and paste code with zero regard from Stack Overflow. So one person's quick example becomes another's code maintenance nightmare.
@JonathonReinhart How would I decode the send data, if using msgpack.dumps, or should I use msgpack.pack? Also it throws an error saying "Cannot serialize <__main__.Test instance at 0x02AF9C10>" if I try to dumps it
could you send executable functions in this way?

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.