connection refused error in paho-mqtt python package - python

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.

I was facing the same issue.
The solution was to install a local MQTT broker.
http://www.steves-internet-guide.com/install-mosquitto-linux/

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)

Related

I have Postgres server connection error when I use docker?

My application works fine on my system but when I build a docker image by docker composer and then run the application it shows the error below.
However, I resolved it by changing the ip address from 127.0.0.1 => 0.0.0.0 in the Flask setting as below and the docker image works fine on my system. But when I run it on another ubuntu it gives me the same error and changes the IP to 127.0.0.1.
if __name__ == "__main__":
app.run(host='**0.0.0.0**',debug=False)
ERROR:
Traceback (most recent call last):
File "./main.py", line 22, in <module>
ps.create_table() # create the table if is not existed
File "/prj/Modules/postgres.py", line 49, in create_table
connection = get_connection()
File "/prj/Modules/postgres.py", line 36, in get_connection
return psycopg2.connect(f"dbname={db_name} user={user} password={password} host={host}")
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host **"127.0.0.1"** and accepting
TCP/IP connections on port 5432?
you can have access to my code and my docker hub:
docker hub: applia65/duplicatebugreportsearchengine
Github: https://github.com/ghasemieh/Duplicated-Bug-Report-Detection-System/blob/master/main.py
Basically I have 3 container my-app, Postgres, and MongoDB.
I appreciate it if you help me with this problem.
Thanks
From the trace, the PostgreSql isn't accessible. Can you try updating your docker compose and add expose statement like below:
expose:
- 5432
This will allow access only to the linked services unless "ports" is also specified.

Connection refused when trying to create a connection

We are using django to call APIs (ubuntu server) that are on 172.20.44.148.
Our code is the following:
import socket
socket.create_connection(('172.20.44.148', 8080), timeout=2)
However, the connection refused.
This is the error we got from apache:
[root#xxx ~]# python /home/something.py
Traceback (most recent call last):
File "/home/something.py", line 3, in <module>
socket.create_connection(('172.20.44.148', 8080), timeout=2)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
We also tried to ping 172.20.44.148 and we got a response.
Any ideas?

Transfer a file from local server to another using Python library - Paramiko

I want to transfer a file from a local server (that'll contain the code and file) to a remote server preferably using ssh. Here's the code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('Servername', port = 135, username='username', password='password')
print "connected successfully!"
sftp = ssh.open_sftp()
print sftp
sftp.put('G:\TestDocument.txt','G:\TestDocument.txt' )
sftp.close()
print "copied successfully!"
ssh.close()
But I get this error:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last): File
"C:/Python27/testtransfer.py", line 5, in
ssh.connect('Servername', port = 135, username='username', password='password') File
"C:\Python27\lib\site-packages\paramiko\client.py", line 392, in
connect
t.start_client(timeout=timeout) File "C:\Python27\lib\site-packages\paramiko\transport.py", line 545, in
start_client
raise e SSHException: Error reading SSH protocol banner
Can you tell me why am I receiving this error? I have purposely used port 135 because port 22 is closed on the target server and 135 (among others) is open.
You can even suggest some other way in which I can transfer files from one server to another using Python.
SSHException: Error reading SSH protocol banner
the error usually means the remote service was not a ssh service. try to make sure the service on port 135 is actually ssh.
And what's the OS on your server? Windows? if so you may need to setup a 3rd-part ssh server on your server. In my opinion if you need to copy files from windows to windows ftp is a much simpler solution. python has a built-in lib ftplib.

Connect to redis from another container in docker

I have app, that used Tornado and tornado-redis. [image "app" in docker images]
I start redis:
docker run --name some-redis -d redis
Then I want to link my app with redis:
docker run --name some-app --link some-redis:redis app
And I have error:
Traceback (most recent call last):
File "./app.py", line 41, in <module>
c.connect()
File "/usr/local/lib/python3.4/site-packages/tornadoredis/client.py", line 333
, in connect
self.connection.connect()
File "/usr/local/lib/python3.4/site-packages/tornadoredis/connection.py", line
79, in connect
raise ConnectionError(str(e))
tornadoredis.exceptions.ConnectionError: [Errno 111] Connection refused
I have tested my code with local tornado and redis, and it works. The problem in
c = tornadoredis.Client()
c.connect()
Why my app cant connet to redis-container? How to fix that? I use standart port 6379.
Thanks!
tornadoredis attempts to use redis on localhost. (See source here)
So you need to inform tornadoredis where redis is running (since to the docker image it is not running on localhost).
For example:
c = tornadoredis.Client(host="<hostname>")
c.connect()
In your specific case, substitute "redis" for "<hostname>".

PyCharm remote debugging

I have remote Ubuntu server, and I'm trying set up remote debugging.
Configured as shown here.
import sys
import pydevd
sys.path.append('/root/home/scripts/pycharm-debug.egg')
pydevd.settrace('my_remote_server_IP', port=51234,
stdoutToServer=True, stderrToServer=True)
I also connect remote host for synchronizing and uploading my python scripts to remote server. (Tools -> Deployment -> ...)
When I start debugging:
C:\Python27\python.exe C:/Projects/python/demo.py
Could not connect to xx.xx.xx.166: 51234
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.1\helpers\pydev\pydevd_comm.py", line 428, in StartClient
s.connect((host, port))
File "C:\Python27\Lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10061] ����������� �� �����������,
Process finished with exit code 1
How to solve this problem?
The first argument of pydevd.settrace function should be the host, where PyCharm is installed. Not remote server.
Also in most cases if you want to run and debug your code remotely it is more convenient to use remote interpreter feature.
I resolved this problem by changing the port.

Categories