protobuf解析问题

单蛙 发布于 2016/12/23 15:37
阅读 284
收藏 0

protobuf 2.6,python2.7


#!/usr/bin/python
#fileencoding:utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import paho.mqtt.client as mqtt
import Pos_pb2
username = "aaa"
pswd = "aaa"
host = "aaa"
port = 1883
topic = "aaa"

def on_connect(mqttc, obj, flags, rc):
    print("rc: "+str(rc))

def on_message(mqttc, obj, msg):
    print(msg.topic+" "+str(msg.qos))
    print list(msg.payload)
    position = Pos_pb2.Position()
    p = position.ParseFromString(msg.payload)
    print p
def on_publish(mqttc, obj, mid):
    print("mid: "+str(mid))

def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

def on_log(mqttc, obj, level, string):
    print(string)

def main():
    # If you want to use a specific client id, use
    # mqttc = mqtt.Client("client-id")
    # but note that the client id must be unique on the broker. Leaving the client
    # id parameter empty will generate a random id for you.
    #def __init__(self, client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp"):
    mqttc = mqtt.Client(protocol=mqtt.MQTTv31)
    mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    # Uncomment to enable debug messages
    #mqttc.on_log = on_log
    mqttc.username_pw_set(username, pswd)
    try:
        mqttc.connect(host, port, 30)
        mqttc.subscribe(topic, 2)
        #mqttc.version(3.1)
        mqttc.loop_forever()
    except Exception as e:
        print e

if __name__ == '__main__':
    main(


接收到的信息的list()形式如下,但是protobuf解析后结果为None,求解答

加载中
返回顶部
顶部