Paho连接问题-Mosquitto:与代理进行通信时发生网络协议错误

时间:2018-11-23 10:45:53

标签: python docker mosquitto paho

  1. 我运行mosquitto(docker映像-在外部服务器上)
  2. 一段时间后,我的蚊子无法工作并显示此错误(我不知道为什么)
  

与代理进行通信时发生网络协议错误。

  1. 当此蚊虫错误发生并且我尝试连接/发布时,(python)paho.mqtt会向我抛出此信息:
  

回溯(最近通话最近一次):

     

_bootstrap_inner中的文件“ /usr/lib/python3.5/threading.py”,第914行,
   self.run()
  运行中的文件“ /usr/lib/python3.5/threading.py”,行862
       self._target(* self._args,** self._kwargs)
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   2913,在_thread_main
中        self.loop_forever(retry_first_connection = True)
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   1578,在loop_forever
       rc = self.loop(超时,max_packets)
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   1072,在循环中
       rc = self.loop_read(max_packets)
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   1374,在loop_read
       rc = self._packet_read()
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   2071,在_packet_read
中        rc = self._packet_handle()
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   2560,在_packet_handle
       返回自我。_handle_publish()
  文件“ /usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py”,行   2728,在_handle_publish中       (主题,数据包)= struct.unpack(pack_format,数据包)struct.error:结构格式错误的字符

  1. 看来我的paho.mqtt.client仍处于连接状态,但是即使我重新启动mosquitto并使用mosquitto_sub(mosquitto错误也消失了),我仍然看不到程序中的消息。我需要重新启动程序,但不能令人满意。

请告诉我,为什么mosquitto向我显示此错误和/或如何捕获(python)paho.mqtt socket.error(有可能吗?)

我认为这部分代码应该足够:)

mqtt_client.py

class MQttClient:
    client = None
    connected_flag = False
    loop_is_working_flag = False
    broker_is_online_flag = True
    subscribe_command_channel_flag = True
    mosquitto_ip = None

    def __init__(self, config):
        self.config = config
        self.logger = Logger(config, self)


    def connect(self, ip_address='127.0.0.1', port=1883, keepalive=4):
        self.mosquitto_ip = ip_address
        self.client = mqtt.Client(self.config.get(REQUIRE_INI_SECTION, DEVICE_ID_INI_PARAMETER), clean_session=False)
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        try:
            self.client.connect(ip_address, port, keepalive=keepalive)
            self.client.loop_start()
            self.wait_for_connect()
            self.loop_is_working_flag = True
        except (OSError, ConnectionRefusedError):
            self.logger.error(CAN_NOT_CONNECT_WITH_IP_ADDRESS_ERROR.format(ip_address), send_to_mosquitto=False)

    def publish_message(self, message, topic, retain=False):
        return self.client.publish(self.config.get(REQUIRE_INI_SECTION, BUILDING_NAME_INI_PARAMETER)
                                   + '/' + topic + '/' +
                                   self.config.get(REQUIRE_INI_SECTION, DEVICE_ID_INI_PARAMETER), message, qos=1,
                                   retain=retain)

    def on_connect(self, client, userdata, flags, rc):
        if rc is 0:
            self.connected_flag = True
            self.logger.info(CONNECTED_TO_THE_MOSQUITO_INFO.format(self.mosquitto_ip), send_to_mosquitto=True)

    def on_disconnect(self, client, userdata, rc):
        if rc is not 0:
            self.connected_flag = False
            self.logger.error(DISCONNECTED_FROM_THE_MOSQUITO_ERROR.format(
                self.mosquitto_ip, str(rc)), send_to_mosquitto=True)

    def wait_for_connect(self):
        while not self.connected_flag:
            pass

    ...

和用法:

controller.py

class Controller:

    def __init__(self):
        self.config = configparser.ConfigParser()
        self.config.read(CONFIG_FILE_PATH)
        self.client = MQttClient(self.config)
        self.__connect_to_mosquitto()

    def __connect_to_mosquitto(self):
        if Network.host_is_available(self.config.get(NETWORK_INI_SECTION, BROKER_IP_INI_PARAMETER)):
            if not self.client.loop_is_working_flag:                
                self.client.connect(ip_address=self.config.get(NETWORK_INI_SECTION, BROKER_IP_INI_PARAMETER))
                time.sleep(0.2)
                self.client.publish_message(self.json_parser.get_json_welcome_data(), MOSQUITTO_TOPIC_WELCOME_DATA)

        else:
            self.logger.error(NO_CONNECTION_WITH_BROKER_ERROR, send_to_mosquitto=False)
            Network.restart_network_interface_if_there_is_no_connection(
                self.config.get(NETWORK_INI_SECTION, ROUTER_IP_INI_PARAMETER))

    ...

    def execute(self):
        while self.client.connected_flag and self.client.broker_is_online_flag:
            ...
            if self.client.publish_message(data, MOSQUITTO_TOPIC_DATA)[0] is not 0:
                ...
                self.logger.error(CAN_NOT_SEND_DATA_TO_MOSQUITTO_ERROR)
                break

我检查了一下,此方法返回0(成功)。但是我没有在mosquitto_sub上收到消息

 self.client.publish_message(data, MOSQUITTO_TOPIC_DATA)[0] 

0 个答案:

没有答案