Sending a Dictionary using Sockets in Python?

前端 未结 6 562
悲哀的现实
悲哀的现实 2021-01-12 06:27

My problem: Ok, I made a little chat program thing where I am basically using sockets in order to send messages over a network.

It works great, but when I decided t

相关标签:
6条回答
  • 2021-01-12 06:39

    Using JSON to serialize your data is the way I prefer to do it. I actually made a library that does just that for you: jsonsocket library. It will do the serialization/deserialization automatically for you. It also handles big amounts of data efficiently.

    0 讨论(0)
  • 2021-01-12 06:49

    You can use pickle and python remote object (or pyro only), to send full objects and data over networks (Internet included). For instance, if you want send object (dict, list, class, objects, etc. ) use python remote objects for it.

    It very useful for you want to do.

    There is more information in this link http://pythonhosted.org/Pyro4/ And this starter manual can be useful to know what you send or execute on network pcs http://pythonhosted.org/Pyro4/intro.html#simple-example

    I hope it will help you

    0 讨论(0)
  • 2021-01-12 06:54

    You have to serialize your data. there would be many ways to do it, but json and pickle will be the likely way to go for they being in standard library.

    for json :

    import json
    
    data_string = json.dumps(data) #data serialized
    data_loaded = json.loads(data) #data loaded
    

    for pickle(or its faster sibling cPickle):

    import cPickle as pickle
    
    data_string = pickle.dumps(data, -1) 
    #data serialized. -1, which is an optional argument, is there to pick best the pickling protocol
    data_loaded = pickle.loads(data) #data loaded.
    

    also, please don't write

    i= True
    while i is True:
     #do_something
    

    because simple while True: would suffice.

    0 讨论(0)
  • 2021-01-12 06:54

    You can also use zmqObjectExchanger (https://github.com/ZdenekM/zmq_object_exchanger). It wraps pickle and zmq to transfer python objects over network.

    0 讨论(0)
  • 2021-01-12 07:03

    You need to serialize your data first. There are several ways to do this, the most common probably JSON, XML and (python specific) pickles. Or your own custom serialization.

    The basic idea is: Serialize your data, send it, receive it, deserialize it again.

    0 讨论(0)
  • 2021-01-12 07:03

    If you want to use pickle you can use the loads and dumps functions.

    import pickle
    a_dict = { x:str(x) for x in range(5) }
    serialized_dict = pickle.dumps(a_dict)
    # Send it through the socket and on the receiving end:
    a_dict = pickle.loads(the_received_string)
    

    You can also use JSON in a similar fashion. I like JSON because it is human readable and isn't python specific.

    import json
    a_dict = { x:str(x) for x in range(5) }
    serialized_dict = json.dumps(a_dict)
    # Send it through the socket and on the receiving end:
    a_dict = json.loads(the_received_string)
    
    0 讨论(0)
提交回复
热议问题