ZeroMQ消息:pyzmq请求响应模式 ( Request-Reply),Python
服务端:
import zmq
import sys
def main():
context = zmq.Context()
skt = context.socket(zmq.REP)
skt.bind("tcp://127.0.0.1:8888")
while True:
try:
print("等待客户连接 ...")
data = skt.recv()
n = int(str(data.decode('utf-8')))
print("收到客户端数据:", n)
msg = n + 1
skt.send(bytes(str(msg), encoding='utf-8'))
except Exception as e:
print('异常:', e)
sys.exit()
if __name__ == '__main__':
main()
输出:
等待客户连接 ...
收到客户端数据: 2020
客户端:
import zmq
import sys
def main():
context = zmq.Context()
print("连接服务器...")
skt = context.socket(zmq.REQ)
skt.connect("tcp://127.0.0.1:8888")
n = '2020'
skt.send(bytes(n, encoding='utf-8'))
msg = skt.recv()
print("接收到服务器响应值: ", msg.decode('utf-8'))
sys.exit()
if __name__ == '__main__':
main()
输出:
连接服务器...
接收到服务器响应值: 2021