当前位置: 首页>>代码示例>>Python>>正文


Python MqttClient.recv方法代码示例

本文整理汇总了Python中mqttcli.MqttClient.recv方法的典型用法代码示例。如果您正苦于以下问题:Python MqttClient.recv方法的具体用法?Python MqttClient.recv怎么用?Python MqttClient.recv使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mqttcli.MqttClient的用法示例。


在下文中一共展示了MqttClient.recv方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_013

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_013(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("sub:{seq}", connect=4)
        sub.subscribe("/test/022/013/+", qos=2)

        pub.publish("/test/022/013/t1", 'hurry', retain=True, qos=1)
        msg = sub.recv()
        if not isinstance(msg, EventPublish) or \
                msg.msg.topic != '/test/022/013/t1' or \
                msg.msg.payload != 'hurry' or\
                msg.msg.qos != 1 or\
                msg.msg.retain:
            debug(msg)
            return False

        # same with empty payload
        pub.publish("/test/022/013/t2", 'up', retain=True, qos=2)
        pub.pubrel(pub.get_last_mid())

        msg = sub.recv()
        if not isinstance(msg, EventPublish) or \
                msg.msg.topic != '/test/022/013/t2' or \
                msg.msg.payload != "up" or\
                msg.msg.qos != 2 or\
                msg.msg.retain:
            debug(msg)
            return False

        return True
开发者ID:gbour,项目名称:wave,代码行数:31,代码来源:022_retain.py

示例2: test_231

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_231(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=1)
        pub.publish("foo/bar", "grrr", qos=1)

        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.payload != "grrr":
            debug(evt)
            return False

        ack = sub.unsubscribe("foo/bar")
        if not isinstance(ack, EventUnsuback):
            debug(ack)
            return False

        sub.puback(evt.msg.mid)
        ack2 = pub.recv()
        if not isinstance(ack2, EventPuback):
            debug(ack2)
            return False

        pub.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:27,代码来源:020_v311.py

示例3: test_202

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_202(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "wootwoot", qos=2, read_response=False)

        # PUB PUBREC
        evt = pub.recv()
        # sending PUBREL with wrong pktid
        pub.forge(NC.CMD_PUBREL, 2, [
            ('uint16', (evt.mid+10)%65535) # wrong pktid
        ], send=True)

        # subscriber: PUBLISH never received
        evt = sub.recv()
        if evt is not None:
            debug(evt)
            return False

        evt = pub.recv()
        # publisher: PUBCOMP never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:30,代码来源:020_v311.py

示例4: test_203

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_203(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "wootwoot", qos=2, read_response=False)

        # PUB PUBREC
        evt = pub.recv()
        pub.pubrel(pub.get_last_mid(), read_response=False)

        # subscr: receiving PUBLISH
        evt = sub.recv()
        sub.pubrec(evt.msg.mid, read_response=False)

        # subscr: receiving PUBREL
        evt = sub.recv()

        # sending PUBCOMP with wrong pktid
        sub.forge(NC.CMD_PUBCOMP, 0, [
            ('uint16', (evt.mid+10)%65535) # wrong pktid
        ], send=True)


        evt = pub.recv()
        # publisher: PUBCOMP never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:34,代码来源:020_v311.py

示例5: test_003

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_003(self):
        sub = MqttClient("sub:{seq}", connect=4)
        sub.subscribe('a/b', qos=1)

        pub = MqttClient("pub:{seq}", connect=4)
        msg = gen_msg()
        pub.publish('a/b', payload=msg, qos=1)
        pub.recv()

        e = sub.recv()
        if not isinstance(e, EventPublish) or \
                e.msg.payload != msg or \
                e.msg.qos     != 1:
            debug('failing event: {0}'.format(e))
            return False

        # send PUBACK
        sub.puback(e.msg.mid)

        puback_evt = pub.recv()
        # PUBACK mid == PUBLISH mid
        # validating [MQTT-2.3.1-6]
        if not isinstance(puback_evt, EventPuback) or \
                puback_evt.mid != pub.get_last_mid():
            debug('failing event: {0}'.format(puback_evt))
            return False

        sub.unsubscribe('a/b')
        sub.disconnect(); pub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:32,代码来源:006_qos1.py

示例6: test_003

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_003(self):
        monitor = MqttClient("monitor:{seq}", connect=4)
        # NOTE: '/' prefix skips $ messages
        # TODO: remove it when '$' filter will be impl.
        monitor.subscribe("/#", qos=0)

        client  = MqttClient("rabbit:{seq}", keepalive=2)
        will    = {'topic': '/node/disconnect', 'message': client.clientid()}
        client.connect(version=4, will=will)

        time.sleep(1)
        client.send_pingreq()
        evt = monitor.recv()
        if monitor.recv() != None:
            debug(evt)
            return False

        time.sleep(4)
        evt = monitor.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \
                evt.msg.payload != will['message']:
            debug(evt)
            return False

        monitor.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:28,代码来源:021_lastwill.py

示例7: test_232

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_232(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "grrr", qos=2)                # receive PUBREC as response
        pub.pubrel(pub.get_last_mid(), read_response=False) # triggers message delivery

        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.payload != "grrr":
            debug(evt)
            return False

        ack = sub.unsubscribe("foo/bar")
        if not isinstance(ack, EventUnsuback):
            debug(ack)
            return False

        rel = sub.pubrec(evt.msg.mid)
        if not isinstance(rel, EventPubrel):
            debug(rel)
            return False

        sub.pubcomp(evt.msg.mid)
        comp = pub.recv()
        if not isinstance(comp, EventPubcomp):
            debug(comp)
            return False

        pub.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:33,代码来源:020_v311.py

示例8: test_012

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_012(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("sub:{seq}", connect=4)
        sub.subscribe("/retain/+", qos=0)

        pub.publish("/retain/delivered", 'waze', retain=True)
        msg = sub.recv()
        if not isinstance(msg, EventPublish) or \
                msg.msg.topic != '/retain/delivered' or \
                msg.msg.payload != 'waze' or\
                msg.msg.retain:
            debug(msg)
            return False

        # same with empty payload
        pub.publish("/retain/empty", '', retain=True)
        msg = sub.recv()
        if not isinstance(msg, EventPublish) or \
                msg.msg.topic != '/retain/empty' or \
                msg.msg.payload != None or\
                msg.msg.retain:
            debug(msg)
            return False

        pub.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:28,代码来源:022_retain.py

示例9: test_020

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_020(self):
        metrics = [
            '$SYS/broker/uptime',
            '$SYS/broker/version',
            '$SYS/broker/clients/total',
            '$SYS/broker/clients/connected',
            '$SYS/broker/clients/disconnected',
            '$SYS/broker/messages/sent',
            '$SYS/broker/messages/received',
            '$SYS/broker/messages/inflight',
            '$SYS/broker/messages/stored',
            '$SYS/broker/publish/messages/sent',
            '$SYS/broker/publish/messages/sent/qos 0',
            '$SYS/broker/publish/messages/sent/qos 1',
            '$SYS/broker/publish/messages/sent/qos 2',
            '$SYS/broker/publish/messages/received',
            '$SYS/broker/publish/messages/received/qos 0',
            '$SYS/broker/publish/messages/received/qos 1',
            '$SYS/broker/publish/messages/received/qos 2',
            '$SYS/broker/retained messages/count',
            '$SYS/broker/subscriptions/count',
        ]

        c = MqttClient("metrics:{seq}", connect=4)
        c.subscribe('$SYS/#', qos=0)

        while True:
            evt = c.recv()
            if evt is not None and isinstance(evt, EventPublish):
                break

        stats = {evt.msg.topic: evt.msg.payload}
        while True:
            evt = c.recv()
            if evt is None: break
            if isinstance(evt, EventPingResp): continue

            stats[evt.msg.topic] = evt.msg.payload

        #pprint(stats)
        for topic in metrics:
            if topic not in stats:
                debug("{0} metric not in $SYS".format(topic))
                return False

            del stats[topic]

        if len(stats) > 0:
            debug("extra $SYS metrics: {0}".format(stats))

        c.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:54,代码来源:033_metrics.py

示例10: _pubsub

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def _pubsub(self, pubtopic, subtopic, match):
        msg = gen_msg()

        sub = MqttClient("sub:{seq}", connect=4)
        suback_evt = sub.subscribe(subtopic, 0)
        if not isinstance(suback_evt, EventSuback) or \
                suback_evt.mid != sub.get_last_mid() or \
                suback_evt.granted_qos[0] != 0:
            if match: debug("failed to subscribe: {0}".format(suback_evt))
            return False

        pub = MqttClient("pub:{seq}", connect=4)
        pub.publish(pubtopic, msg)
        pub.disconnect()

        e = sub.recv()
        unsuback_evt = sub.unsubscribe(subtopic)
        if not isinstance(unsuback_evt, EventUnsuback) or \
                unsuback_evt.mid != sub.get_last_mid():
            if match: debug("failed to unsubscribe: {0}".format(unsuback_evt))
            return False

        sub.disconnect()
        #print e, e.msg.topic, e.msg.payload
        if not isinstance(e, EventPublish) or \
                e.msg.topic   != pubtopic or \
                e.msg.payload != msg:
            if match: debug("invalid received msg: {0}".format(e))
            return False

        return True
开发者ID:gbour,项目名称:wave,代码行数:33,代码来源:002_pubsub.py

示例11: test_022

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_022(self):
        retain = MqttClient("retain:{seq}", connect=4)
        sub    = MqttClient("subscriber:{seq}", connect=4)

        topic = "/woot/wo/ot"; msg = "expression of simplistic ecstasy"
        retain.publish(topic, msg, retain=True)

        # wildcard match topic
        sub.subscribe("/woot/#", qos=0, read_response=False)
        acked = False; pubevt = None
        while True:
            evt = sub.recv()
            if isinstance(evt, EventSuback):
                acked = True; continue

            # receiving retained message
            if isinstance(evt, EventPublish) and\
                    evt.msg.topic   == topic and\
                    evt.msg.payload == msg   and\
                    evt.msg.retain:
                pubevt = evt; continue

            break

        if not acked:
            debug("not acked")
            return False
        if pubevt is None:
            debug("message not received")
            return False

        retain.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:35,代码来源:022_retain.py

示例12: test_251

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_251(self):
        tfs = [
            ("+"         , True),
            ("/+"        , True),
            ("+/"        , True),
            ("+/foo"     , True),
            ("/foo/+"    , True),
            ("/foo/+/"   , True),
            ("/foo/+/bar", True),
            ("+/foo/bar" , True),
            ("foo+"      , False),
            ("foo+/bar"  , False),
            ("+foo/bar"  , False),
            ("foo/+bar"  , False),
            # ~
            ("++"        , False),
            ("foo/++/bar", False),
        ]

        for (tf, isvalid) in tfs:
            sub = MqttClient("conformity:{seq}", connect=4)
            sub.subscribe(tf, qos=0, read_response=False)
            ack = sub.recv()

            if (isvalid and not isinstance(ack, EventSuback)) or \
                    (not isvalid and (ack is not None or sub.conn_is_alive())):
                debug("{0}: {1} ({2})".format(tf, ack, sub.conn_is_alive()))
                return False

            sub.disconnect()

        return True
开发者ID:gbour,项目名称:wave,代码行数:34,代码来源:020_v311.py

示例13: test_002

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_002(self):
        sub = MqttClient("sub:{seq}", connect=4)
        sub.subscribe("foo/bar", qos=1)

        pub = MqttClient("pub:{seq}", connect=4)
        pub.publish("foo/bar", env.gen_msg(42), qos=1)

        evt = sub.recv()
        if not isinstance(evt, EventPublish):
            debug(evt)
            defer.returnValue(False)

        # PUBACK not send
        sub.destroy(); del sub

        if (yield supervisor.count('wave_msgworkers_sup')) != 1:
            debug("wrong msgworkers count")
            defer.returnValue(False)

        # msg worker is destroyed after 5 secs
        time.sleep(6)
        if (yield supervisor.count('wave_msgworkers_sup')) != 0:
            debug("wrong msgworkers count")
            defer.returnValue(False)

        pub.disconnect()
        defer.returnValue(True)
开发者ID:gbour,项目名称:wave,代码行数:29,代码来源:031_partial_delivery.py

示例14: test_008

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_008(self):
        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 28),        # will=1, will-qos=3
            ('uint16', 60),        # keepalive
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 12),        # will=1, will-qos=1
            ('uint16', 60),        # keepalive
            ('string', client._c.client_id),   # clientid
            ('string', '/foo/bar'),# will topic
            ('uint16', 0),         # will payload len
            ('bytes' , ''),        # will payload
        ], send=True)

        evt = client.recv()
        if not isinstance(evt, EventConnack):
            debug(evt)
            return False

        client.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:33,代码来源:021_lastwill.py

示例15: test_030

# 需要导入模块: from mqttcli import MqttClient [as 别名]
# 或者: from mqttcli.MqttClient import recv [as 别名]
    def test_030(self):
        retain = MqttClient("retain:{seq}", connect=4)
        sub    = MqttClient("subscr:{seq}", connect=4)

        msg = {'topic': "baby/ma/ma", 'payload': "The mother of your child(ren)", 'retain': True,
                'qos': 1}
        retain.publish(**msg)

        sub.subscribe("baby/ma/+", qos=1, read_response=False)

        pubevt = None
        while True:
            evt = sub.recv()
            if isinstance(evt, EventSuback): continue
            if isinstance(evt, EventPublish) and\
                    evt.msg.qos == 1 and\
                    evt.msg.retain and\
                    evt.msg.topic == msg['topic'] and\
                    evt.msg.payload == msg['payload']:
                pubevt = evt; continue

            break

        if pubevt is None:
            debug("message not received")
            return False
        sub.puback(mid=pubevt.msg.mid)

        retain.disconnect(); sub.disconnect()
        return True
开发者ID:gbour,项目名称:wave,代码行数:32,代码来源:022_retain.py


注:本文中的mqttcli.MqttClient.recv方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。