0

To preface I'm very new to python (about 7 days) but I'm an experienced software eng undergrad.

I would like to send data between machines running python scripts. The idea I had (in order to simplify things) was to concatenate the data (strings & ints) into a string and do the parsing client-side.

The UDP packets send beautifully with simple strings but when I try to send useful data python always complains about the data I send; specifically python won't let me concatenate tuples.

  • In order to parse the data on the client I need to seperate the data with a dash character: '-'.
  • nodeList is of type dictionary where the key is a string and value is a double.

    randKey = random.choice( nodeList.keys() )
    data = str(randKey) +'-'+ str(nodeList[randKey])
    mySocket.sendto ( data , address ) 
    

The code above produces the following error:

TypeError: coercing to Unicode: need string or buffer, tuple found

I don't understand why it thinks it is a tuple I am trying to concatenate...

So my question is how can I correct this to keep Python happy, or can someone suggest I better way of sending the data?

Thank you in advance.

1
  • what python version are you running, what line number (and line) is the error pointing to. Commented Oct 31, 2011 at 20:01

3 Answers 3

1

I highly suggest using Google Protocol Buffers as implemented in Python as protobuf for this as it will handle the serialization on both ends of the line. It has Python bindings that will allow you to easily use it with your existing Python program.

Using your example code you would create a .proto file like so:

message SomeCoolMessage {
    required string key = 1;
    required double value = 2;
}

Then after generating, you can use it like so:

randKey = random.choice( nodeList.keys() )
data = SomeCoolMessage()
data.key = randKey
data.value = nodeList[randKey]
mySocket.sendto ( data.SerializeToString() , address ) 
Sign up to request clarification or add additional context in comments.

2 Comments

I was just about to write up a suggestion for Protocol Buffers myself!
Yeah I really like it; makes serializing things like this a breeze.
1

I'd probably use the json module serialize the data.

Comments

0

You need to serialize the data. Pickle does this built in for you, and you can ask pickle for an ascii representation of the data vs binary data (see the docs), or you could use json (it also serializes the data for you) both are in the standard library. But really there are a hundred thousand different libraries that handle ALL the work for you, in getting data from 1 machine to another. I'd suggest using a library.

Depending on speed, etc. there are different trade offs for the various libraries. In the standard library you get HTTP, that's about it (well and raw sockets). But there are others. If super fast speed is more important than other things..., zeroMQ, or google's protocol buffers might be valid options.

For me, I use rpyc usually, it lets me be totally lazy, and just call over to the other process across the network. It's fast enough usually.

You know that UDP has no guarantee that the data will ever show up on the other side, or that it will show up IN ORDER. for your application you may not care, I don't know, but just thought I'd bring it up.

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.