我从github下载zguide源代码。 我发现python示例code-wuserver.py不起作用。 运行wuserver.py和wuclient.py,wuclient.py无法收集任何数据,但如果我运行c代码-wuserver.c,wuclient.py可以收集数据。
wuserver.py
#
# Weather update server
# Binds PUB socket to tcp://*:5556
# Publishes random weather updates
#
import zmq
from random import randrange
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")
while True:
zipcode = randrange(1, 100000)
temperature = randrange(-80, 135)
relhumidity = randrange(10, 60)
socket.send_string("%i %i %i" % (zipcode, temperature, relhumidity))
wuclient.py
#
# Weather update client
# Connects SUB socket to tcp://localhost:5556
# Collects weather updates and finds avg temp in zipcode
#
import sys
import zmq
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print("Collecting updates from weather server...")
socket.connect("tcp://localhost:5556")
# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)
# Process 5 updates
total_temp = 0
for update_nbr in range(5):
string = socket.recv_string()
zipcode, temperature, relhumidity = string.split()
total_temp += int(temperature)
print("Average temperature for zipcode '%s' was %dF" % (
zip_filter, total_temp / update_nbr)
)
答案 0 :(得分:2)
我试过了,没有任何错误。您可以修改wuclient.py
代码以查看已转移的内容:
for update_nbr in range(5):
string = socket.recv_string()
zipcode, temperature, relhumidity = string.split()
print(string) # add this statement
total_temp += int(temperature)
然后,您应首先运行wuclient.py
,然后wuserver.py
。
P.S。:zguide文档提供了一种非常清晰的方法来调试丢失的消息问题,这可能非常有用。这是Missing Message Problem Solver