Connecting to an Ubuntu Server with Python - python

My goal is to have a basic FTP program written in Python. First, I need to gain more knowledge. My question is, how can I connect to an Ubuntu Server (hosted via VirtualBox) using Python?
I have tried using the page on the official Python website but I get an error saying socket.error: [Errno 61] Connection refused when using this
from ftplib import FTP
ftp = FTP('jordan#10.0.0.12')
This is the output I get when using ftp - FTP('10.0.0.12')
Traceback (most recent call last): File "", line 1, in
File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py",
line 120, in init
self.connect(host) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py",
line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout) File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 575, in create_connection
raise err socket.error: [Errno 61] Connection refused
I can use an FTP program such as Transmit (the same port and on SFTP) on the same machine and it works fine.

Python's ftplib does not support SFTP, so using PySFTP would work.

An alternative is using paramiko.

Related

Python FTP: "TimeoutError: [Errno 110] Connection timed out" but I can connect with sftp in terminal

I'm getting error while connecting to FTP in Python:
server.connect('68.183.91.171')
File "/usr/lib/python3.6/ftplib.py", line 152, in connect
source_address=self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
My code:
import ftplib
server = ftplib.FTP()
server.connect('68.183.91.171')
server.login('root','password')
server.dir()
I think the connection settings are correct as I can connect in terminal with
sftp 68.183.91.171
You are connecting with an SFTP client in the console.
While you are connecting with an FTP library in Python.
FTP and SFTP are completelly different protocols. If you want to replicate your SFTP connection in Python, you need to use a Python SFTP module, like Paramiko or pysftp.

Python ftplib - Errno 10061- No connection could be made because the target machine actively refused it

I want to establish a FTP connection to Windows server (let's say 10.20.30.50)
I am using Python ftblib package
session = ftplib.FTP('10.20.30.40','admin','password#123')
file = open('download.jpg','rb') # file to send
session.storbinary('STOR download.jpg', file) # send the file
file.close() # close file and FTP
session.quit()
But I am getting the error
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 120, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it.
I tried telnet 10.20.30.40 8081 and it is successful.
I allow port 8081 for ftp. I open port by adding new inbound rules.
I tried several available solutions on Stack Overflow as well as other sites but they all are talking about to check the port. I do all the things but still no help.
Thanks
If your FTP server uses a non-standard port (8081), you have to specify that, when using FTP class.
session = ftplib.FTP()
session.connect('10.20.30.40', 8081)
session.login('admin','password#123')
See also Python ftplib - specify port.
I forgot to enable FTP service in server. I followed this link and it is solved my issue
https://medium.com/#JohGeoCoder/allowing-ftp-access-on-windows-server-2016-hosted-on-microsoft-azure-af25898df958

python and phoenixdb on Ubuntu (or Windows). How to?

I have created a python script connecting to a Phoenix HBase to analyze some data. I want to set this script up on crontab on a ubuntu server that I have running.
The script is perfectly able to run on my Windows 10 machine. But when I try to use the phoenixdb connector on Ubuntu I get an error on RunTime.
>>> import phoenixdb
>>> url = '<some-url>'
>>> conn = phoenixdb.connect(url, autocommit=True)
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.5/site-packages/phoenixdb/avatica.py", line 156, in connect
self.connection.connect()
File "/usr/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ubuntu/.local/lib/python3.5/site-packages/phoenixdb/__init__.py", line 63, in connect
client.connect()
File "/home/ubuntu/.local/lib/python3.5/site-packages/phoenixdb/avatica.py", line 158, in connect
raise errors.InterfaceError('Unable to connect to the specified service', e)
phoenixdb.errors.InterfaceError: ('Unable to connect to the specified service', TimeoutError(110, 'Connection timed out'), None, None)
I was hoping someone here knows a way to fix this problem?
I am running Python 3.6 on Windows and Python 3.5.2 on Ubuntu, but I doubt that that is the problem.
EDIT:
I have now started a Windows 2012 Server and have tried setting my script up here as well, and it seems not to be a problem solely for Ubuntu. I am getting the exact same error on Windows.
>>> import phoenixdb
>>> url = '<some-url>'
>>> conn = phoenixdb.connect(url, autocommit=True)
Traceback (most recent call last):
File "C:\Users\Administrator\Anaconda3\lib\site-packages\phoenixdb\avatica.py", line 156, in connect
self.connection.connect()
File "C:\Users\Administrator\Anaconda3\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\Administrator\Anaconda3\lib\socket.py", line 722, in create_connection raise err
File "C:\Users\Administrator\Anaconda3\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Administrator\Anaconda3\lib\site-packages\phoenixdb\__init__.py", line 63, in connect
client.connect()
File "C:\Users\Administrator\Anaconda3\lib\site-packages\phoenixdb\avatica.py", line 158, in connect
raise errors.InterfaceError('Unable to connect to the specified service', e)
phoenixdb.errors.InterfaceError: ('Unable to connect to the specified service',
TimeoutError(10060, 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond', None, 10060, None), None, None)
I recently did format the PC that I developed the script on. The was using this phoenixdb connector, and I did not experience a similar problem on that.
I did also try to install Python 3.6 on the Windows machine (similar to the same python version that I installed on my normal PC - the one I developed the script on).
I'm really lost for finding a solution..
I finally found the problem. It had nothing to do with the machines I set up my script on. It had to do with security settings on the AWS Machines that both the Ubuntu and Windows Servers were.

Using SSH and Paramiko in Python to connect to a remote server fails

I have recently downloaded and installed SSH from https://pypi.python.org/pypi/ssh/1.7.14 for my Python 2.7 64-bit
Then I tried to connect to a server which is a virtual machine on the network. I have full authorization to get access to it. My code is:
import ssh
import paramiko
if __name__ == '__main__':
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.92.15.12', username = "testUser", password = "testPwd")
However, I've got following error:
Traceback (most recent call last):
File "C:\Users\testUser\Documents\Aptana Studio 3 Workspace\testProj\testRemoteExecution.py", line 56, in <module>
ssh.connect('10.92.15.12', username = "testUser", password = "testPwd")
File "C:\Python27\lib\site-packages\paramiko\client.py", line 283, in connect
sock.connect((hostname, port))
File "C:\Python27\lib\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
So did I make any mistake here?
Thanks.

a way to use Paramiko ssh / scp functionality from Unix to Windows or Windows to Windows?

is there a way to use Python paramiko package in order to scp or ssh from Unix to Windows or from Windows to Windows ?
It works very well from Windows to Unix and from Unix to Unix but not for the other configurations.
My code is
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect ('192.168.0.10', 22, 'username', 'pwd')
transport = client.get_transport ()
channel = transport.open_session ()
channel.set_combine_stderr (True)
channel.exec_command ("dir C:/")
I get the following error message
Traceback (most recent call last):
File "paramiko_test_scp.py", line 50, in
client.connect ('192.168.0.10', 22, 'username', 'pwd')
File "C:\Python27\lib\site-packages\paramiko\client.py", line 291, in connect
sock.connect(addr)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061]
Windows socket error 10061 means : no connection, target machine actively refused it.
- Is your SSH server running (are you using OpenSSH ?)
- Try to disable your firewall

Categories

Resources