Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Vikyath Ram

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.

con=FTP(hostname)

The FTP class supports following methods −

connect()

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

Getwelcome()

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()

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'.

Storbinary()

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

dir()

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

delete(filename)

Remove the file named filename from the server.

cwd(pathname)

Set the current directory on the server.

mkd(pathname)

Create a new directory on the server.

FTP.pwd()

Return the pathname of the current directory on the server.

rmd(dirname)

Remove the directory named dirname on the server.

size(filename)

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.

Quit()

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)
   ftp.quit()
   localfile.close()
def uploadFile():
   filename = '/home/malhar/file.txt'
   ftp.storbinary('STOR '+filename, open(filename, 'rb'))
   ftp.quit()
with FTP("ftp1.at.proftpd.org") as ftp:
   ftp.login()
   ftp.getwelcome()
   ftp.dir()
   downloadFile()
   uploadFile()

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.