FTP protocol client in Python

The all important The FTP class in ftplib module implements the client side of the FTP protocol.

To establish connection with a FTP server, obtain FTP object.


The FTP class supports following methods −


Connect to the given host and port. The default port number is 21, as specified by the FTP protocol specification.


Return the welcome message sent by the server in reply to the initial connection.

login(user='anonymous', passwd='', acct='')

Log in as the given user. The passwd and acct parameters are optional and default to the empty string. If no user is specified, it defaults to 'anonymous'. If user is 'anonymous', the default passwd is 'anonymous@'.


Abort a file transfer that is in progress.

retrbinary(cmd, callback, blocksize=8192, rest=None)

Retrieve a file in binary transfer mode. cmd should be an appropriate RETR command: 'RETR filename'.


Store a file in binary transfer mode. cmd should be an appropriate STOR command: "STOR filename". fp is a file object (opened in binary mode) which is read until EOF using its read() method


Produce a directory listing as returned by the LIST command, printing it to standard output.


Remove the file named filename from the server.


Set the current directory on the server.


Create a new directory on the server.


Return the pathname of the current directory on the server.


Remove the directory named dirname on the server.


Request the size of the file named filename on the server. On success, the size of the file is returned as an integer, otherwise None is returned. Note that the SIZE command is not standardized, but is supported by many common server implementations.


Send a QUIT command to the server and close the connection.

Following example establishes anonymous connection with a server, downloads a file to local folder and uploads a local file.

from ftplib import FTP
import os
def downloadFile():
   filename = 'README.MIRRORS'
   localfile = open(filename, 'wb')
   ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
def uploadFile():
   filename = '/home/malhar/file.txt'
   ftp.storbinary('STOR '+filename, open(filename, 'rb'))
with FTP("ftp1.at.proftpd.org") as ftp:
Vikyath Ram
Vikyath Ram

A born rival