Subscriber does not receive message, Pyzmq
I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub
pattern not sure how to incorporate that idea.
This is my codes:
Publisher:
import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)
Subscriber:
context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())
In terms of raw UDP, I wrote a code which works perfectly.
broadcast:
def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)
sock.sendto(msg.encode(), (Host,port))
recv:
def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)
python sockets zeromq pyzmq
add a comment |
I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub
pattern not sure how to incorporate that idea.
This is my codes:
Publisher:
import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)
Subscriber:
context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())
In terms of raw UDP, I wrote a code which works perfectly.
broadcast:
def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)
sock.sendto(msg.encode(), (Host,port))
recv:
def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)
python sockets zeromq pyzmq
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37
add a comment |
I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub
pattern not sure how to incorporate that idea.
This is my codes:
Publisher:
import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)
Subscriber:
context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())
In terms of raw UDP, I wrote a code which works perfectly.
broadcast:
def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)
sock.sendto(msg.encode(), (Host,port))
recv:
def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)
python sockets zeromq pyzmq
I am trying to publish a message(it's like broadcast when using raw sockets) to my subnet with a known port but at subscriber end, the message is not received. The idea is the IP address of the first machine should not be known to the second machine that's why I am using broadcast IP. With UDP or TCP raw socket, it works but I am trying to learn pub-sub
pattern not sure how to incorporate that idea.
This is my codes:
Publisher:
import zmq
import sys
import time
context=zmq.Context()
socket=context.socket(zmq.PUB)
socket.bind("tcp://192.168.1.255:5677")
while True:
data='hello'.encode()
socket.send(data)
#time.sleep(1)
Subscriber:
context=zmq.Context()
sub=context.socket(zmq.PUB)
sub.setsocketopt(zmq.SUBSCRIBE, "".encode())
sub.connect('tcp://192.168.1.255:5677')
sub.recv()
print(sub.recv())
In terms of raw UDP, I wrote a code which works perfectly.
broadcast:
def broadcast(Host,port):
#send bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
msg=get_ip_data("wlp3s0")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
time.sleep(1.5)
# print("yes sending", client)
sock.sendto(msg.encode(), (Host,port))
recv:
def broadcast_recv():
#listen bd
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((get_bd_address("wlp1s0"),12345))
# receive broadcast
msg, client = sock.recvfrom(1024)
a=(msg.decode())
print(a)
python sockets zeromq pyzmq
python sockets zeromq pyzmq
edited Nov 21 '18 at 22:24


Benyamin Jafari
2,96832139
2,96832139
asked Nov 20 '18 at 16:23
keerthanakeerthana
378
378
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37
add a comment |
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37
add a comment |
1 Answer
1
active
oldest
votes
You forgot the zmq.SUB
in the subscriber side. And you typed sub.setsocketopt()
instead of sub.setsockopt()
.
Try it:
Publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
Subscriber:
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[NOTE]:
- You can also change the
.bind()
and.connect()
in subscriber and publisher with your policy. (This post is relevant). - Make sure that
5677
is open in the firewall.
socket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthanasocket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397299%2fsubscriber-does-not-receive-message-pyzmq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You forgot the zmq.SUB
in the subscriber side. And you typed sub.setsocketopt()
instead of sub.setsockopt()
.
Try it:
Publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
Subscriber:
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[NOTE]:
- You can also change the
.bind()
and.connect()
in subscriber and publisher with your policy. (This post is relevant). - Make sure that
5677
is open in the firewall.
socket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthanasocket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
add a comment |
You forgot the zmq.SUB
in the subscriber side. And you typed sub.setsocketopt()
instead of sub.setsockopt()
.
Try it:
Publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
Subscriber:
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[NOTE]:
- You can also change the
.bind()
and.connect()
in subscriber and publisher with your policy. (This post is relevant). - Make sure that
5677
is open in the firewall.
socket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthanasocket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
add a comment |
You forgot the zmq.SUB
in the subscriber side. And you typed sub.setsocketopt()
instead of sub.setsockopt()
.
Try it:
Publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
Subscriber:
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[NOTE]:
- You can also change the
.bind()
and.connect()
in subscriber and publisher with your policy. (This post is relevant). - Make sure that
5677
is open in the firewall.
socket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
You forgot the zmq.SUB
in the subscriber side. And you typed sub.setsocketopt()
instead of sub.setsockopt()
.
Try it:
Publisher:
import zmq
import time
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5677") # Note.
while True:
socket.send_string('hello')
time.sleep(1)
Subscriber:
context = zmq.Context()
sub=context.socket(zmq.SUB) # Note.
sub.setsockopt(zmq.SUBSCRIBE, b"") # Note.
sub.connect('tcp://192.168.1.255:5677')
while True:
print(sub.recv())
[NOTE]:
- You can also change the
.bind()
and.connect()
in subscriber and publisher with your policy. (This post is relevant). - Make sure that
5677
is open in the firewall.
socket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
edited Nov 25 '18 at 7:11
answered Nov 21 '18 at 22:40


Benyamin JafariBenyamin Jafari
2,96832139
2,96832139
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthanasocket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
add a comment |
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthanasocket.bind("tcp://*:5677")
orsocket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.
– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
Hi, thanks for your script. I was also thinking if broadcasting is possible in zmq. @benyamin jafari
– keerthana
Nov 23 '18 at 16:53
@keerthana
socket.bind("tcp://*:5677")
or socket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.– Benyamin Jafari
Nov 23 '18 at 16:55
@keerthana
socket.bind("tcp://*:5677")
or socket.bind("tcp://0.0.0.0:5677")
is broadcasting trick.– Benyamin Jafari
Nov 23 '18 at 16:55
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
Thanks, you answered my pub-sub doubts so checked it as answered, though i want to broadcast to subnet address. So i cannot use 0.0.0.0 address but use 192.X.X.255 address and where the subscriber does not receive the data. :(
– keerthana
Nov 24 '18 at 10:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397299%2fsubscriber-does-not-receive-message-pyzmq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Doesn’t seem to me that you have based your code on the pyzmq examples, such as github.com/IntelPython/source-publish/tree/master/pyzmq/… 0 why not?
– barny
Nov 21 '18 at 22:37