欢迎访问宙启技术站
智能推送

使用Python生成随机的zmqDEALER消息

发布时间:2023-12-11 12:16:30

在Python中使用zmq库生成随机的zmq DEALER消息非常简单。下面是一个生成随机消息的示例代码:

import zmq
import random
import time

context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.bind("tcp://*:5555")

messages = ["Hello", "World", "Python", "ZMQ", "Random", "Message"]

while True:
    msg = random.choice(messages)
    socket.send_string(msg)
    print("Sent message: ", msg)
    time.sleep(1)

上面的代码创建了一个zmq的上下文对象和一个DEALER类型的socket。然后,程序生成一个随机的消息并发送给socket。程序会每秒发送一条消息。

通过运行上面的代码,你可以在你的终端中看到随机消息的输出结果。

Sent message: Hello
Sent message: ZMQ
Sent message: Python
Sent message: Random
Sent message: Message
...