Enable Dark Mode!
what-is-ftp-and-sftp-in-python.jpg
By: Sruthi M Kumar

What is FTP and SFTP in Python

Technical

File Transfer Protocol (FTP)

File Transfer Protocol(FTP) are used to transfer files. Python has a built-in package for FTP which is called ftplib. ftplib is used to transfer computer files between a client and a server across a computer network. These are of two types: Active FTP connection and Passive FTP connection. We can transfer files using stream, block, and compressed modes. It works on port 21.

Python ftplib

Python ftplib is a module that allows transferring files via the FTP protocol.

Python FTP class

The ftplib.FTP creates a new instance of the FTP class. When the host is given, a connection to the host is made with the connect method.

Methods in FTP class

FTP.connect(host[, port[, timeout]])

Connect to the given host and port.

FTP.getwelcome()

This will gives the welcome message sent by the server in reply to the initial connection.

FTP.login([user[, passwd[, acct]]])

Log in as the given user.

FTP.delete(filename)

Remove the filename from the server.

FTP.cwd(pathname)

Set the current directory on the server.

FTP.mkd(pathname)

Create a new directory on the server.

FTP.pwd()

Return the pathname of the current directory.

FTP.quit()

This will send a QUIT command to the server, and it will close the connection.

FTP.close()

Closes the connection.

Log in to an FTP server and List files

import ftplib
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
When we run this, we get the output as,
- lrwxrwxrwx 1 0    0            1 Nov 13  2012 ftp -> .
- lrwxrwxrwx 1 0    0            3 Nov 13  2012 mirror -> pub
- drwxr-xr-x   23 0    0        4096 Dec 14  2018 pub
- drwxr-sr-x   89 0    450      4096 Nov 08 19:37 site
- drwxr-xr-x 9 0    0        4096 Jan 23  2014 vol

Change the Directory

import ftplib
ftp = ftplib.FTP("ftp.nluug.nl")
ftp.login("anonymous", "ftplib-example-1")
data = []
#change directory to /pub/
ftp.cwd('/pub/')       
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
When we run this, we get the output as,
- lrwxrwxrwx 1 504  450        14 Nov 02  2007 FreeBSD -> os/BSD/FreeBSD
- lrwxrwxrwx 1 504  450        20 Nov 02  2007 ImageMagick -> graphics/ImageMagick
- lrwxrwxrwx 1 504  450        13 Nov 02  2007 NetBSD -> os/BSD/NetBSD
- lrwxrwxrwx 1 504  450        14 Nov 02  2007 OpenBSD -> os/BSD/OpenBSD
- -rw-rw-r-- 1 504  450        932 Jan 01  2019 README.nluug
- -rw-r--r-- 1 504  450      2023 May 03  2005 WhereToFindWhat.txt
- drwxr-sr-x 2 0    450      4096 Jan 26  2008 av
- drwxrwsr-x 2 0    450      4096 Aug 12  2004 comp
- drwxrwsr-x 2 0    450      4096 Mar 24  2000 crypto
- drwxr-xr-x 2 500  450      4096 Apr 10  2014 db
- lrwxrwxrwx 1 0    0          21 Feb 22  2017 debian -> os/Linux/distr/debian
- drwxrwsr-x 2 0    450      4096 Mar 29  2009 documents
- drwxrwsr-x 2 0    450      4096 Dec 01  2008 editors
- -rw-r--r-- 1 0    0          43 Jun 15  2013 favicon.ico
- lrwxrwxrwx 1 0    450          1 Nov 02  2007 ftp -> .

SSH File Transfer Protocol (SFTP)

SFTP is a secure file transfer protocol. Which stands for SSH file transfer protocol, is a way for transferring files between machines over a secure and Encrypted connection. Which is the advanced version of FTP, and it works on port 22. The main features are, It will encrypt the data and provides authentication to username and password.
Python pysftp module is a simple interface to SFTP.
To install this module:
pip install pysftp
Or
python3 -m pip install pysftp
Login to a remote server using sftp 
import pysftp
with pysftp.Connection('hostname', username='me', password='secret') as sftp:
    with sftp.cd('/code'):        
        sftp.put('/pycode/filename') 
        sftp.get('remote_file')     
Which will give the list of files present in the code directory and also put and get some files in that directory.
FTP does not provide a secure channel for moving documents between hosts, while SFTP offers a secure channel for file transfer between hosts on a network.
FTP uploads/downloads data without any security. In the case of SFTP, it provides full security for data to authenticate SSH protocol. It also uses SSH keys to authenticate.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message