软件:
pip install pyzmq
代码:
==server.py
#
# Hello World server in Python
# Binds REP socket to tcp://*:5555
# Expects "Hello" from client, replies with "World"
#
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
# Wait for next request from client
message = socket.recv()
print ("Received request: ", message)
# Do some 'work'
time.sleep (1) # Do some 'work'
# Send reply back to client
socket.send_string("World")
==client.py
#
# Hello World client in Python
# Connects REQ socket to tcp://localhost:5555
# Sends "Hello" to server, expects "World" back
#
import zmq
context = zmq.Context()
# Socket to talk to server
print ("Connecting to hello world server..." )
socket = context.socket(zmq.REQ)
socket.connect ("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range (1,10):
print ("Sending request ", request,"..." )
socket.send_string ("Hello")
# Get the reply.
message = socket.recv()
print ("Received reply ", request, "[", message, "]" )
步骤
打开一个命令行,执行python server.py
打开一个命令行,执行python client.py
参考:
https://blog.csdn.net/kent45/article/details/10397917