zeromy quick start - python
阿新 • • 發佈:2018-06-12
AC eply conn exp 打開 cal pip ring 命令行 # 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")
# 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, "]" )
軟件:
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:
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
# 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
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
zeromy quick start - python