Socket nodename error with Python Fabric module - python

I am attempting to run a simple command (ls) using the fabric module by following the examples found in the fabric documentation. However, when I attempt to call the run method on a Connection object, I receive
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
import fabric
c = fabric.Connection('host')
c.run('ls')
I have utilized the socket module before, and the error above is generally raised when running socket.gethostbyname(socket.gethostname()) to bind a socket to a machine. However, socket.gethostbyname(socket.gethostname()) runs without error for me.
How can I successfully use fabric to run commands in the shell? As fabric utilizes SSH, are there any SSH configurations needed before using fabric?
I am running fabric on macOS Sierra 10.12.5 on Python 3.7.0

In the Connection constructor, you need to give the real host name.
For instance:
import fabric
c = fabric.Connection('localhost')
c.run('ls')
If the SSH protocole is not configured, you have another error message. For instance:
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 127.0.0.1, ::1 or fe80::1%lo0
Ofen, the server name is not enough, you need to add the domain name, for instance myserver.mydomain.com.

Related

Connecting to localhost using paramiko in python always gives error?

I am new to paramiko module for python. I want to make a remote ssh connection to a client. For now I am trying to connect to the localhost using the paramiko module but every time running the python script gives the No Valid Connection error? How can I solve this?
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1')
This is the error I get running this script.
raise NoValidConnectionsError(errors)
paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 22 on 127.0.0.1

Python mod_wsgi database connection not working

I've installed Postgres, mod_wsgi and python3.5 on a centos system. I've tested the connection and it works fine both in the interactive Python shell as well as the development application (it's a Pyramid app so it's the dev server from that).
I'm getting the following error:
postgresql.exceptions.ClientCannotConnectError: could not establish connection to server
CODE: 08001
LOCATION: CLIENT
CONNECTION: [failed]
failures[0]:
socket('127.0.0.1', 5432)
Traceback (most recent call last):
File "/var/www/wsgi/lib/python3.3/site-packages/postgresql/protocol/client3.py", line 136, in connect
self.socket = self.socket_factory(timeout = timeout)
File "/var/www/wsgi/lib/python3.3/site-packages/postgresql/python/socket.py", line 64, in __call__
s.connect(self.socket_connect)
PermissionError: [Errno 13] Permission denied
The above exception was the direct cause of the following exception:
postgresql.exceptions.ConnectionRejectionError: Permission denied
CODE: 08004
LOCATION: CLIENT
CONNECTOR: [Host] pq://username:***#localhost:5432/db_name
category: None
DRIVER: postgresql.driver.pq3.Driver
I spent some time looking at the mod_wsgi documentation and found there is a possible issue with sockets. However, I've already implemented that change (and the corresponding error from apache vanished).
My assumption is that it's a socket error still as the connection sting works fine elsewhere in the same Python install and even within the dev setup.
Can anybody point me in the right direction from here please?
Even if you set the database connect string to an IP address, if it is 127.0.0.1, I have encountered PostgreSQL database connectors which will try and optimise things and connect via the local UNIX socket instead. If the user that your code runs under with mod_wsgi cannot access that UNIX socket for accessing PostgreSQL it will fail with a permissions error.
I can't remember whether it was py-postgresql module that I saw this with, but did hit it a couple of weeks ago with on of the PostgreSQL database modules.
Try replacing 127.0.0.1 with the actual host name or proper IP address.

why connection always gets refused?

I am trying to connect machine using the RPyC but it always says that connection refused.
I did on the python shell
import rpyc
rpyc.connect("hostname", port)
but it says connection refused. checked the firewall for the port. firewall allow this port.
Try using the exact same versions of both python and rpyc on client and server !
This means that you are not runnin the server side for rpyc
you need to donwload the source code for rpyc from here
https://github.com/tomerfiliba-org/rpyc/releases
then run:
python bin/rpyc_classic.py
where bin is in the source code folder
Once you have that running, you should be able to run the python code without any issues
I hope it works
default server binds to localhost, but the client needs to have hostname 'None' to do this correctly:
rpyc.connect(None, port)

Connection refused FTP python 2.7

I am using the ftplib module and want to connect to FTP on my localhost. I am using the following code example:
import ftplib
f = ftplib.FTP()
f.connect("localhost")
f.login()
ls=[]
f.retrlines('MLSD',ls.append)
for entry in ls:
print entry
It gives me a socket.error: [Errno 61] Connection refused
How do i connect to my localhost and what am i doing wrong? Eventually i would want to send files from a client code to server code using ftplib, both running on the same machine. This is for a project, i know in reality you wouldn't have both on the same machine.
I am guessing the socket.error exception occurs on line 3, where you have connect().
Connection refused indicates that the operating system or the firewall rejected the connection. The most likely reason is that you don't actually have an FTP server running on your local machine.
You didn't say whether you do or you don't, and you didn't say what operating system you're using. But by default, operating systems don't come with an FTP server installed and running. So you need to install a compatible FTP server. A good one to consider is FileZilla Server.

Connecting to Python XML RPC from the Mac

I wrote an XML RPC server in python and a simple Test Client for it in python. The Server runs on a linux box. I tested it by running the python client on the same linux machine and it works.
I then tried to run the python client on a Mac and i get the following error
socket.error: (61, 'Connection Refused')
I can ping and ssh into the linux machine from the Mac. So i dont think its a configuration or firewall error.
Does anyone have any idea what could be going wrong?
The code for the client is as below:
import xmlrpclib
s = xmlrpclib.ServerProxy('http://143.252.249.141:8000')
print s.GetUsers()
print s.system.listMethods()
"Connection Refused" means the connection was REFUSED - the machine 143.252.249.141 is up, and in the network, but is not accepting connections on port 8000 - it is actively refusing them.
So maybe the server software isn't running on the server? Or is running in another port? Or is bound to a different IP address?

Categories

Resources