How configure a Multiple PUB/ single SUB python ZMQ Ubuntu

喜夏-厌秋 提交于 2020-12-15 06:26:07

问题


I have two VMs (VirtualBOx, Ubuntu 18.04 and python-zmq[16.0.2-2build2]) running within the same physical machine (Win10). Both machines are configured as Bridge and they can be ping successfully 192.168.1.66-192.168.1.55. I've followed this tutorial https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html. It works if PUB (server) is configured as

import zmq
import random
import sys
import time

port = "5557"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
while True:
    topic = random.randrange(9999,10005)
    messagedata = random.randrange(1,215) - 80
    print "%d %d" % (topic, messagedata)
    socket.send("%d %d" % (topic, messagedata))
    time.sleep(1)

And the SUB (client) as

import sys
import zmq

port = "5557"
if len(sys.argv) > 1:
    port =  sys.argv[1]
    int(port)
    
if len(sys.argv) > 2:
    port1 =  sys.argv[2]
    int(port1)

# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print "Collecting updates from weather server..."
socket.connect ("tcp://192.168.1.66:%s" % port)

if len(sys.argv) > 2:
    socket.connect ("tcp://localhost:%s" % port1)
# Subscribe to zipcode, default is NYC, 10001
topicfilter = "10001"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)

# Process 5 updates
total_value = 0
for update_nbr in range (5):
    string = socket.recv()
    topic, messagedata = string.split()
    total_value += int(messagedata)
    print topic, messagedata

print "Average messagedata value for topic '%s' was %dF" % (topicfilter, total_value / update_nbr)

Since I want a single client (SUB) with multiple servers (PUB) where they can be hundreds even thousands, it is unfeasible to configure a single IP for each PUB. Is there a way to subscribe without specifying the IP? Or at least a broadcast one. I've tried to configure at client in socket.connect ("tcp://IP:%s" % port):

"*"

Gives the error:

Traceback (most recent call last):
  File "sub_client.py", line 18, in <module>
    socket.connect ("tcp://*:%s" % port)
  File "zmq/backend/cython/socket.pyx", line 528, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:5980)
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:8400)
zmq.error.ZMQError: Invalid argument

192.168.1.1 (GW), 192.168.1.255 (broadcast), localhost/127.0.0.1 and it's IP (192.168.1.55) -> does not receive messages

192.168.1.66 (server's IP) -> Does receive messages but not practical in a large scale system

Any way to solve this?


回答1:


Q : Any way to solve this?

Avoid going against any API-documented property. While a .bind()-method can for the tcp://-transport-class try to indeed bind to any localhost-side IP-addresses, the .connect()-method, for obvious reasons can't.

As was notified in the ZMQError:

socket.connect ("tcp://*:%s" % port)
zmq.error.ZMQError: Invalid argument

Correct the IP-address target, where shall the
.connect( "tcp://{0:}:{1:}".format( IP, PORT ) )-method try to "ring-so-as-to-get-connection".



来源:https://stackoverflow.com/questions/64918425/how-configure-a-multiple-pub-single-sub-python-zmq-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!