Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: send a list/dict over network

I'm looking for an easy way of packing/unpacking data structures for sending over the network:

on client just before sending:

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

and then on server:

a = unpack(message)

Is there a library that could do pack/unpack magic? Thanks in advance

like image 334
facha Avatar asked Dec 08 '22 03:12

facha


1 Answers

Looks like JSON might fit the bill. It's simple, and it's in the Python standard library.

It might not be too happy about the tuples, though:

>>> 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]]

Whether or not that's a problem is for you to decide.

like image 171
Thomas Avatar answered Dec 09 '22 17:12

Thomas