【问题标题】:PYZMQ not receiving message between Python 2.7 and Python 3.5PYZMQ 未在 Python 2.7 和 Python 3.5 之间接收消息
【发布时间】:2016-11-10 01:39:09
【问题描述】:

python 2.7 和 python 3.5 之间的字节数组和 str 类型似乎是 pyzmq PUB/SUB 的问题。 我必须在 python 2.7 和 python 3.5 中发布/订阅代理一个。 我有一个订阅者同时订阅了 pub/sub 代理,但它没有收到所有已发布的消息。 如何让我的 pub/sub 代理订阅并重新发布在 IP:Port 上发布的所有消息?

示例代码:

    def subscribeformessages(self):
        context = zmq.Context(1)
        xsub = context.socket(zmq.SUB)
        xsub_url = "tcp://%s:%s" % (self.ipaddress, self.xsub_url)
        xsub.setsockopt_string(zmq.SUBSCRIBE, '')
        xsub.setsockopt(zmq.SUBSCRIBE, b'')
        if not is_py2:
            xsub.setsockopt_string(zmq.SUBSCRIBE, "")
        else:
            xsub.setsockopt(zmq.SUBSCRIBE, "")
            xsub.setsockopt(zmq.SUBSCRIBE, b"")
            xsub.setsockopt_unicode(zmq.SUBSCRIBE, u"", encoding='utf-8')
            xsub.setsockopt_string(zmq.SUBSCRIBE, u"")
    xsub.connect(xsub_url)
    try:
        while self.running:
            try:
                time.sleep(.2)
                receive = xsub.recv_multipart()
                self.print_message_queue.put("sub recv\'d: %s" % receive)
                self.pub_local_que.put(receive)
                self.publish_queue.put(receive)
            except zmq.ZMQError as err:
                print(err)
           ....

发布者示例:

    def sendtopicerequesttoexchange(self):
        context = zmq.Context(1).instance()
        sock = context.socket(zmq.PUB)
        sock.linger = 0
        try:
            sock.bind("tcp://ip:port")
        except:
            sock.connect("tcp://ip:port")

        topicxml = xmltree.Element("MessageXML")
        topicxml.attrib['NodeAddr'] = '040000846'
        topicxml.attrib['Payload'] = 'HeyBob'
        replymsg = xmltree.tostring(topicxml)
        msg = [str.encode("send.downlink"), str(replymsg).encode('utf-8')]
        msg[0] = str(msg[0]).encode('utf-8')
        try:
            count = 0
            while True:
                time.sleep(4)
                sock.send_multipart(msg)
                print("msg %s" %(msg))
                count += 1
                if count > 1:
                    break
            time.sleep(.2)
        except Exception as bob:
            print(bob)
        finally:
            time.sleep(5)
            sock.setsockopt(zmq.LINGER, 0)
            sock.close()

有什么想法吗?

【问题讨论】:

标签: python python-3.x zeromq pyzmq


【解决方案1】:

我在这里找到了答案:http://pyzmq.readthedocs.io/en/latest/pyversions.html

我将出版物更改为:

        def sendtoexchange_pete(self):
            context = zmq.Context(1).instance()
            sock = context.socket(zmq.PUB)
            sock.linger = 0
            try:
                sock.bind("tcp://ip:port")
            except:
                sock.connect("tcp://ip:port")

            topicxml = xmltree.Element("Downlink_75")
            topicxml.attrib["NodeAddr"] = "$301$0-0-0-040000846"
            topicxml.attrib["Payload"] = "HeyBob Pete\'s Xchnge 75"
            replymsg = xmltree.tostring(topicxml)
            # By changing to force bytes I was able to get the to work
            msg = [b'send.downlink', b'%s' % replymsg]
            try:
                count = 0
                while True:
                    time.sleep(4)
                    sock.send_multipart(msg)
                    print("msg %s" %(msg))
                    .....

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 2017-07-08
    • 2017-03-13
    • 2018-03-24
    • 1970-01-01
    • 2017-06-28
    • 2021-09-24
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多