Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to transport an object in twisted?

I know with "transport.write()" I can transport string object but I want to know is this possible to transport other type of data, something like a python class object ? if It is possible how can I do this?

like image 501
nim4n Avatar asked Jan 04 '14 12:01

nim4n


2 Answers

You can devise some arbitrary custom protocol, but it may be more productive to use a framework that already exists for this purpose, like google's protocol buffers that allow you to easily define an efficient message passing structure, and support python already.

JSON is an easy alternative, and plenty of people simply use zipped json objects, because its easy and built in to python by default, but the results are slow, have unpredictable size artifacts (zipped is typically larger than uncompressed output for numerous small messages), and are a poor solution for transferring binary data.

edit: Oh yes, and don't use pickle.

like image 198
Doug Avatar answered Oct 30 '22 09:10

Doug


You can use json to serialize the Objects. Almost all Python objects are json serializable and if not then you can write your own encoder-decoder to handle them. Don't use eval to decode the input though.

like image 23
Ashwini Chaudhary Avatar answered Oct 30 '22 10:10

Ashwini Chaudhary