Prinshu Kumar
Prinshu Kumar

Reputation: 33

connection refused error in paho-mqtt python package

I am new to paho-mqtt. I was trying to publish a topic using my localhost and I encountered the following error :

Traceback (most recent call last):
  File "server.py", line 10, in <module>
    client1.connect(host,port,keepalive)
  File "/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.1-py2.7.egg/paho/mqtt/client.py", line 768, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.1-py2.7.egg/paho/mqtt/client.py", line 895, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

My python code is below :

import paho.mqtt.client as paho
port=1883
host = "localhost"
keepalive = 60
def on_publish(client,userdata,result):
    print("data published \n")
    pass
client1= paho.Client("control1")
client1.on_publish = on_publish
client1.connect(host,port,keepalive)
ret= client1.publish("Robot","Robot 1 move_left")

When I run the same code with iot.eclipse.org as host then it works fine. Any help would be highly appreciated.

Upvotes: 3

Views: 10592

Answers (2)

yacov drori
yacov drori

Reputation: 1

The exposed docker port for mqtt is usually different than 1883. I use the official eclipse mosquitto docker and the run example on their page is something like:

sudo docker run -it -p 11883:1883 -p 9001:9001 eclipse-mosquitto

therefore the client should connect to port 11883

client.connect(broker_address, 11883)

Upvotes: 0

Murtaza Patrawala
Murtaza Patrawala

Reputation: 181

I was facing the same issue. The solution was to install a local MQTT broker.

http://www.steves-internet-guide.com/install-mosquitto-linux/

Upvotes: 2

Related Questions