Jay Hack
Jay Hack

Reputation: 139

Python zeromq not successfully sending messages (netstat -anf -inet lists it as LISTEN)

I have the following code:

import zmq
import time

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

while True:
    time.sleep (1)
    socket.send("Hello, World")

for some reason it does not seem to be correctly sending the message; when i run

'netstat -anf inet'

in bash, it returns the following:

 Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
 ...
 tcp4       0      0  *.5555                 *.*                    LISTEN 
 ...

From googling around, it looks like "LISTEN" all the way on the right means that it's not actually binding correctly. (This entire line goes away when i shut down the python script mentioned above, and there is no other mention of 5555.) Any ideas what is going wrong here? Thanks!

Upvotes: 0

Views: 606

Answers (1)

minrk
minrk

Reputation: 38588

There will be no network traffic if you have no connected SUB sockets with active subscriptions. Subscriptions are filtered PUB-side, so if there are no active subscribers for a message when you call send, it just discards the message. You should see network traffic if you add a subscriber:

sub = context.socket(zmq.SUB)
sub.connect("tcp://127.0.0.1:5555")
sub.setsockopt(zmq.SUBSCRIBE, b'')

Upvotes: 1

Related Questions