连接到在远程服务器上运行的mosca broker

时间:2019-11-16 11:07:04

标签: python mqtt paho mosca

我创建了一个mosca mqtt经纪人。代码

var mosca = require('mosca');
var settings = {
        port:7474
        }

var server = new mosca.Server(settings);

server.on('ready', function(){
console.log("ready");
});

我使用节点filename.js运行此代码,其启动并运行。我在远程服务器上有此代码。现在,我正在尝试使用python客户端连接到此代理

代码

import paho.mqtt.client as mqtt #import the client1
import json
import pandas as pd


####################################################
def on_message(client, userdata, message):
    print("message received ")


def on_publish(client,userdata,message):
    print("published.")
def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK Returned code=",rc)
    else:
        print("Bad connection Returned code=",rc)

    print("Subscribing to topic","data/#")
    client.subscribe("data/#")


def on_disconnect(client, userdata, rc):
   print("Client Got Disconnected")
   print('rc value'+str(rc))
   if rc != 0:
       print('Unexpected MQTT disconnection. Will auto-reconnect')

   else:
       print('rc value: ' + str(rc))



broker_address=""
port = 7474
print("creating new instance")
client = mqtt.Client(protocol = mqtt.MQTTv311, clean_session=True) #create new instance
client.on_connect = on_connect
client.on_message = on_message #attach function to callback
client.on_disconnect = on_disconnect

print("connecting to broker")
client.connect(broker_address, port=port,) #connect to broker
client.loop_forever() #stop the loop

但是我遇到了错误,

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

我还尝试通过

打开服务器上的端口
sudo ufw allow 7474/tcp 

,但仍然出现相同的错误。 有人可以帮我弄这个吗 。

谢谢

0 个答案:

没有答案