PYZMQ没有接收到python2.7和python3.5之间的消息

2024-09-30 06:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

Python2.7和Python3.5之间的字节数组和str类型似乎是pyzmq PUB/SUB的问题。 我必须发布/订阅代理一个是python2.7,另一个是python3.5。 我有一个订阅了两个发布/订阅代理的订阅服务器,但它并不接收所有发布的消息。 如何让我的发布/订阅代理订阅并重新发布发布发布在IP:端口上的所有消息?在

样本代码:

    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)
           ....

发布者示例:

^{pr2}$

有什么想法吗?在


Tags: self消息url代理stringputcontextzmq
1条回答
网友
1楼 · 发布于 2024-09-30 06:32:17

我在这里找到了答案: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))
                    .....

相关问题 更多 >

    热门问题