连接拒绝与mqtt服务器连接

时间:2018-06-20 09:34:18

标签: python-3.x ssl flask mqtt paho

python paho客户端无法与运行tls的mqtt broker连接。这是我的代码。我尝试了很多方法但都失败了。我希望你们能帮忙

from flask import Flask
import paho.mqtt.client as mqtt
import ssl
app = Flask(__name__)




def on_connect(self,client, userdata, rc):
    print("connected")
    self.subscribe('iot/ac')
    self.subscribe('iot/deviceid/+/temperature')
def on_message(client, userdata, msg):
    print(msg.payload.decode())

def iotac(client, userdata, message):
    print(client)
    print("hellooooo:"+ message.payload.decode())
    client.publish('iot/deviceid/dndkj/temperature',"hiii")
def iotk(client, userdata, message):
    print(message.topic)
    print("temperature:"+ message.payload.decode())


if __name__ == "__main__":
    client = mqtt.Client(client_id="aa", clean_session=True, userdata=None,transport="tcp")
    client.on_connect = on_connect
    client.on_message = on_message
    client.message_callback_add("iot/ac",iotac)
    client.message_callback_add("iot/deviceid/+/temperature",iotk)
    client.tls_set("ca.crt", tls_version=ssl.PROTOCOL_TLSv1_2)
    client.connect("address",8883)
    client.loop_start()

    app.run()

当我运行此程序时,出现错误

File "/usr/lib/python3.5/socket.py", line 702, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

0 个答案:

没有答案