Come configurare un multiplo PUB / singolo SUB python ZMQ Ubuntu

Nov 19 2020

Ho due VM (VirtualBOx, Ubuntu 18.04 e python-zmq [16.0.2-2build2]) in esecuzione sulla stessa macchina fisica (Win10). Entrambe le macchine sono configurate come Bridge e possono eseguire correttamente il ping 192.168.1.66-192.168.1.55. Ho seguito questo tutorialhttps://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html. Funziona se PUB (server) è configurato come

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)

E il SUB (client) come

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)

Poiché desidero un singolo client (SUB) con più server (PUB) dove possono essere centinaia anche migliaia, non è possibile configurare un singolo IP per ogni PUB. C'è un modo per iscriversi senza specificare l'IP? O almeno uno broadcast. Ho provato a configurare sul client in socket.connect ("tcp://IP:%s" % port):

"*"

Fornisce l'errore:

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 e il suo IP (192.168.1.55) -> non riceve messaggi

192.168.1.66 (IP del server) -> Riceve messaggi ma non è pratico in un sistema su larga scala

Qualche modo per risolvere questo problema?

Risposte

user3666197 Nov 19 2020 at 22:21

D : Qualche modo per risolvere questo problema?

Evita di andare contro qualsiasi proprietà documentata dall'API. Mentre un .bind()-method può per la tcp://classe -transport -provare di legarsi effettivamente a qualsiasi localhostindirizzo IP -side, il .connect()-method, per ovvie ragioni, non può.

Come è stato notificato nel ZMQError:

socket.connect ("tcp://*:%s" % port)
zmq.error.ZMQError: argomento non valido

Correggere la destinazione dell'indirizzo IP, dove il
.connect( "tcp://{0:}:{1:}".format( IP, PORT ) )metodo deve tentare di "squillare per ottenere la connessione".