Book Image

Learning Python Network Programming

By : Dr. M. O. Faruque Sarker, Samuel B Washington, Sam Washington
Book Image

Learning Python Network Programming

By: Dr. M. O. Faruque Sarker, Samuel B Washington, Sam Washington

Overview of this book

Table of Contents (17 chapters)
Learning Python Network Programming
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Transferring files with FTP


Unlike SFTP, FTP uses the plain-text file transfer method. This means any username or password transferred through the wire can be detected by an unrelated third-party. Even though FTP is a very popular file transfer protocol, people frequently use this for transferring a file from their PCs to the remote servers.

In Python, ftplib is a built-in module used for transferring the files to and from the remote machines. You can create an anonymous FTP client connection with the FTP() class.

ftp_client = ftplib.FTP(path, username, email)   

Then you can invoke the normal FTP commands, such as CWD. In order to download a binary file, you need to create a file-handler such as the following:

file_handler = open(DOWNLOAD_FILE_NAME, 'wb')

In order to retrieve the binary file from the remote host, the syntax shown here can be used along with the RETR command:

ftp_client.retrbinary('RETR remote_file_name', file_handler.write)

In the following code snippet, an example of a full FTP...