njouanin / hbmqtt

MQTT client/broker using Python asynchronous I/O
MIT License
795 stars 188 forks source link

no message in subscriber #212

Open Nehru-Wairokpam opened 4 years ago

Nehru-Wairokpam commented 4 years ago

pliz solved this bugs

Nehru-Wairokpam commented 4 years ago

import logging import asyncio from hbmqtt.broker import Broker from hbmqtt.client import MQTTClient, ClientException from hbmqtt.mqtt.constants import QOS_1,QOS_2

import paho.mqtt.client as mqtt

client = mqtt.Client()

logger = logging.getLogger(name)

config = { 'listeners': { 'default': { 'type': 'tcp', 'bind': '127.0.0.1:1888' # 0.0.0.0:1883 } }, 'auth':{ 'allow-anonymous': True }, 'sys_interval': 10, 'topic-check': { 'enabled': False } }

broker = Broker(config)

@asyncio.coroutine def startBroker(): yield from broker.start()

async def uptime_coro(): C = MQTTClient() await C.connect('mqtt://127.0.0.1:1888/') await C.subscribe([('Nehru/test', QOS_1)]) try: while 1: message = await C.deliver_message() packet = message.publish_packet

proc_to_arctic(packet.variable_header.topic_name, packet.payload.data.decode('utf8'))

            print(packet.variable_header.topic_name, packet.payload.data.decode('utf8'))

        await C.unsubscribe([TOPIC])
        await C.disconnect()

    except ClientException as ce:
        logger.error("Client exception: %s" % ce)

if name == 'main': formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s" logging.basicConfig(level=logging.INFO, format=formatter) asyncio.get_event_loop().run_until_complete(startBroker()) asyncio.get_event_loop().run_until_complete(uptime_coro()) asyncio.get_event_loop().run_forever()

lanhaosmile commented 4 years ago

I'm not sure if it is the issue description. While starting the broker with config of topic_check disabled and without any check plugins, the add_subscription function @broker.py line 613 thrown the exception KeyError. No message in subcriber since subcription faild to be added.

The KeyError might be caused by None value of topic_plugins at the following lines @ broker.py. line 583 returns = yield from self.plugins_manager.map_plugin_coro( "topic_filtering", session=session, topic=topic, filter_plugins=topic_plugins)

ShenTengTu commented 4 years ago

I have same problem for with topic_check disabled.

There is stacktrace as below if removing try-catch in add_subscription() @broker.py:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/websockets/server.py", line 191, in handler
    await self.ws_handler(self, path)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/broker.py", line 340, in ws_connected
    yield from self.client_connected(listener_name, WebSocketsReader(websocket), WebSocketsWriter(websocket))
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/broker.py", line 470, in client_connected
    result = yield from self.add_subscription(subscription, client_session)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/broker.py", line 626, in add_subscription
    permitted = yield from self.topic_filtering(session, topic=a_filter)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/broker.py", line 588, in topic_filtering
    filter_plugins=topic_plugins)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/plugins/manager.py", line 209, in map_plugin_coro
    return (yield from self.map(self._call_coro, coro_name, *args, **kwargs))
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/plugins/manager.py", line 183, in map
    ret_list = yield from asyncio.gather(*tasks, loop=self._loop)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/plugins/manager.py", line 195, in _call_coro
    return (yield from coro)
  File "/usr/lib/python3.7/asyncio/coroutines.py", line 120, in coro
    res = func(*args, **kw)
  File "/home/pi/projects/CONTRIBUTING/hbmqtt/hbmqtt/plugins/topic_checking.py", line 75, in topic_filtering
    allowed_topics = self.topic_config['acl'].get(username, None)
KeyError: 'acl'

topic_filtering() @broker.py still invoke topic_checking plugin if topic-check disabled, because topic_filtering() only keeps the keyword argument filter_plugins as None.

And in the topic_checking plugin, it doesn't check whether topic filtering is disabled.

ShenTengTu commented 4 years ago

The current solution is to add the acl field to the configuration.

broker.yaml
# ...
topic-check:
  enabled: False
  acl:
    anonymous: ["the_topic/#", "..."]
    user_name: ["another_topic/#", "..."]
# ...
Nolven commented 3 years ago

Same here, spend a whole hecking day trying to make this, ehhhh, thing, work, but found no better solution than settin' ac1

FlorianLudwig commented 3 years ago

This might be related to this issue here: https://github.com/Yakifo/amqtt/pull/28

Jaasdsa commented 3 years ago

this is keng!