python ftp client
Python hosting: Host, run, and code Python in the cloud!
This article will show you how to use the File Transfer Protocol (FTP) with Python from a client side perspective. We use ftplib, a library that implements the FTP protocol. Using FTP we can create and access remote files through function calls.
Related course
Python Programming Bootcamp: Go from zero to hero
Directory listing
FTP is a protocol for transferring files between systems over a TCP network. It was first developed in 1971 and has since been widely adopted as an effective way to share large files over the internet. File Transfer Protocol (often abbreviated FTP) is an application- layer protocol.
We can list the root directory using this little snippet:
import ftplib |
This will output the directory contents. in a simple console style output. If you want to show a specific directory you must change the directory after connecting with the ftp.cwd(‘/‘) function where the parameter is the directory you want to change to.
import ftplib |
Download file
To download a file we use the retrbinary() function. An example below:
import ftplib |
Uploading files
We can upload files using the storlines() command. This will upload the file README.nluug in the main directory. If you want to upload in another directory combine it with the cwd() function.
import ftplib |
Related course
Python Programming Bootcamp: Go from zero to hero
Other functions
For other functions please refer to the official library documentation.
Leave a Reply:
And the synchronisation of data is automatically during all the opening script?
No, it's just an FTP client (file transfer protocol). You can get and put files to an FTP server, but the files are not synchronous. The protocol itself does not support synchronisation. You are looking for something like async instead. The *ftplib* module lets you connect to any FTP server and upload and download files.
To be more elaborate, The FTP protocol is part of the TCP/IP protocol suite and is a means of transfering files from one host to another over the Internet, either public or private. It is an elegant but very simple protocol, and many libraries are available for programming languages. The Python standard library includes a module ftplib which implements the FTP protocol in Python. Python supports the ftplib module.
The module provides two classes for FTP: The ftp.FTP() class is a subclass of socket.socket() and defines an FTP connection but should be used only for communications with passive FTP servers. The ftplib.FTP() class defines a complete FTP client and is used to communicate with both active and passive FTP servers.