How to send a list through TCP sockets - Python

前端 未结 2 1470
深忆病人
深忆病人 2021-02-06 13:24

I want to send a list through TCP sockets but i can\'t get the exact list when receiving from the server-side . To be more specific say that i have this list:

 y         


        
2条回答
  •  余生分开走
    2021-02-06 13:35

    Sorry for my extremely late response, but I hope this can help anyone else who has this problem. This may not be the best solution, but I would convert the list into a string, and encode and send that string. Then, the receiving end can receive, decode, and convert this string back into a list using eval(). This would look something like this:

    Sender:

    y = [0,12,6,8,3,2,10] 
    # Convert To String
    y = str(y)
    # Encode String
    y = y.encode()
    # Send Encoded String version of the List
    s.send(y)
    

    Receiver:

    data = connection.recv(4096)
    # Decode received data into UTF-8
    data = data.decode('utf-8')
    # Convert decoded data into list
    data = eval(data)
    

提交回复
热议问题