- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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()
- Related Articles
- 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://
- XMLRPC server and client modules in Python
- What is FTP?
- PHP FTP context options
- What is the FTP?
- Foreign Trade Policy (FTP)
- Difference between TELNET and FTP
- Difference between FTP and TFTP
- What are the FTP Connections?
- What are the FTP Operations?