ftplib connects to SFTP server without error

Question:

I created a complete FTP library some time ago. Now I want to connect to an SFTP server. As far as I found out in my research this is not possible using ftplib. Nevertheless I tried to connect to an SFTP only server and it worked without any problems. No error and no exception!

Now I’m a bit confused as I’m not sure if this really is a secure connection (which is important for this application). Especially as I could not find similar cases online. Which brings me to the following questions:

  1. How can I find out if the connection is encrypted? (current status: It’s not encrypted)
  2. If the connection is not encrypted: How can I support SFTP without having to recode my complete library?

This is the base code I use:

server = 'host-adress'
user = 'username'
password='password'                                                               
ftp = FTP(server)
ftp.login(user,password)

All operations like up and downloading files work just fine.
According to my host the server is configured as SFTP.

Logging returns this as result:

*cmd* 'USER username'
*put* 'USER usernamern'
*get* '331 Password required for usernamen'
*resp* '331 Password required for username'
*cmd* 'PASS ********'
*put* 'PASS ********rn'
*get* '230 User username logged inn'
*resp* '230 User username logged in'
Asked By: frameworker

||

Answers:

FTP and SFTP are two completely different protocols. They have absolutely nothing in common. There’s no chance you can connect with an FTP library to an SFTP server.

Your log shows FTP session without any doubt.

Though you (or your host) may mistake SFTP with FTPS (FTP over TLS/SSL). See FTPS versus SFTP. For FTPS, just use FTP_TLS class instead of FTP. The interface is the same (FTP_TLS derives from FTP).

If you really need SFTP (over SSH), you need to use a different library, like Paramiko or pysftp.
See SFTP in Python? (platform independent).

Answered By: Martin Prikryl
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.