46045-syslab/pubsub/pub_server.py

27 lines
707 B
Python

import zmq
import sys
import time
port = "5556"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
# Make a context which we use to make sockets from
context = zmq.Context()
# Make a new socket. We want to publish on this socket.
socket = context.socket(zmq.PUB)
# Bind the socket (inside our program) to a port (on our machine)
# We can now send messages
socket.bind(f"tcp://*:{port}")
topic = "TIME"
while True:
# Time to publish the latest time!
messagedata = time.ctime()
# Note the use of XXX_string here;
# the non-_stringy methods only work with bytes.
socket.send_string(f"{topic};{messagedata}")
print(f"Published topic {topic}: {messagedata}")
time.sleep(1)