简体   繁体   中英

How can I send two variables in python socket

I am wondering How to send two variables in python socket. I am currently doing it but the way in which I am sending them can make it so if it misses one send then they switch which variable is which, which is very bad for my purpose. Is there a way I can label the variables so I send both kinda as if I were to label a variable y and on x send the variable with the label and then separate it on the other side, Is there a way to do this?

This question is more about communication protocols than about python programming.

If you want reliable communication, use TCP sockets rather than UDP. Also, encode your messages in a form that keeps the identity of what you're sending together with the values.

JSON based example:

import json
sock.send(json.dumps({"x":x, "y":y}))

Also, you could consider a communications framework like ZeroMQ to take care of the data delivery for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM