Long Smith
Long Smith

Reputation: 1401

Python send data structure over TCP

I want to send something like data structure over tcp it might be class in Python. For example in C I have structure like this:

struct mstruct {
    uint8_t b1;
    uint8_t b2;
    uint8_t b3;
    uint8_t b4;
}; 

And if I want to send it over tcp I can use pointer to the structure. Send all 4 bytes with it and on the receiving side read them and cast to the same structure. Is it any way to do it in Python? If not what is the best solution for doing things like that?

Upvotes: 1

Views: 1072

Answers (2)

user3835277
user3835277

Reputation:

You can pickle objects before they are sent, and then un-pickle them on the other side. Be wary of endianness.

Upvotes: 0

Akisame
Akisame

Reputation: 774

For simple data structures you could use built-in struct library. It's pretty not bad until your data structures becomes huge.

For more declarative and cleaner approach you could try this one. It's built on top of the struct library, but provides a higher level of abstraction.

Upvotes: 1

Related Questions