- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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) 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()
- POP3 protocol client in Python
- SMTP protocol client in Python (smtplib)
- File Transfer Protocol (FTP) in Application Layer
- Difference between File Transfer Protocol (FTP) and Secure File Transfer Protocol (SFTP)
- How to use FTP in Python?
- PHP ftp://
- What is FTP?
- PHP FTP context options
- What is the FTP?
- Protocol and Protocol Hierarchies
- Difference between FTP and TFTP
- Difference between TELNET and FTP
- What are the FTP Operations?
- What are the FTP Connections?
- XMLRPC server and client modules in Python