2016-11-09 8 views
0

python 2.7とpython 3.5の間のバイト配列とstrの型がpyzmq PUB/SUBの問題であるようです。 私はpython 2.7とpython 3.5の中の1つをpub/sub brokerにする必要があります。 両方のpub/subブローカーに加入しているが、発行されたすべてのメッセージを受信して​​いないサブスクライバーがあります。 pub/subブローカーに登録してそこに公開されているすべてのメッセージを再発行するにはどうすればいいですか?Python 2.7とPython 3.5の間でPYZMQがメッセージを受信しない

サンプルコード:

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

任意のアイデア?

+0

は、ここに答えが見つかりました:http://pyzmq.readthedocs.io/en/latest/pyversions.htmlをバイト配列b '%s'を強制的に実行してください。 – user2106070

答えて

0

私はここに答えが見つかりました:私は出版物を変更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)) 
        ..... 
関連する問題