假设你有三个 机器(M1,M2,M3) 具有相同子网和定义端口的三个不同IP地址。我们希望在每个订阅者(M1,M2)上发布消息(来自M1),因此我们将使用以下代码段:
import zmq import time context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind("tcp://*:9999") # Note. while True: socket.send_string('hello-all') time.sleep(1)
context = zmq.Context() sub=context.socket(zmq.SUB) sub.setsockopt(zmq.SUBSCRIBE, b"") sub.connect('tcp://Machine1_IP:9999') # Note while True: print(sub.recv())