Python: send / dict list over network

I am looking for an easy way to pack / unpack data structures to send over the network:

on the client before sending:

a = ((1,2),(11,22,),(111,222)) message = pack(a) 

and then on the server:

 a = unpack(message) 

Is there a library that could make a pack / unpack magic? thanks in advance

+4
source share
3 answers

It looks like JSON might match the score. It is simple, and it is in the Python standard library .

Perhaps he is not too happy with the tuples:

 >>> import json >>> a = ((1,2),(11,22,),(111,222)) >>> print a ((1, 2), (11, 22), (111, 222)) >>> message = json.dumps(a) >>> message '[[1, 2], [11, 22], [111, 222]]' >>> b = json.loads(message) >>> b [[1, 2], [11, 22], [111, 222]] 

Regardless of whether you solve this problem.

+12
source

See pickle - Serializing Python Objects:

The pickle module implements a fundamental but powerful algorithm for serializing and de-serializing the structure of a Python object. Etching is the process by which the hierarchy of Python objects is converted to a byte stream, and unpickling is the reverse operation by which the byte stream is converted back to the object hierarchy. However, etching (and scattering) is alternatively known as “serialization”, “sorting” or “smoothing”, however, to avoid confusion, the terms “etching” and “scattering” are used here.

+2
source

ast.literal_eval() saves tuples:

 >>> a = ((1,2),(11,22,),(111,222)) >>> s = repr(a) >>> import ast >>> ast.literal_eval(s) ((1, 2), (11, 22), (111, 222)) 
+1
source

Source: https://habr.com/ru/post/1305788/


All Articles