Python, FTP, and PythonFTP
1. HTTP is mainly used to download Web-based files and access Web services. Generally, the client can access files and services on the server without logon. Most HTTP file transfer requests are used to obtain a webpage (that is, to download a webpage file to a local device ).
2. FTP is mainly used to download public files anonymously or transfer files between two computers. FTP requires that you enter the user name and password to access the FTP server, but users without accounts are allowed to log on anonymously. However, the Administrator must first set up an FTP server to allow anonymous users to log on. In this case, the anonymous user name is "anonymous" and the password is generally the user's email address.
Workflow:
① The client connects to the FTP server on the remote host.
② Enter the user name and password (or "anonymous" and email address) on the client ).
③ The client performs various file transmission and information query operations.
④ The client exits from the remote FTP server and ends the transmission.
If the client has not responded for more than 15 minutes (900 seconds), the FTP connection times out and is interrupted. At the underlying layer, FTP only uses TCP instead of UDP. FTP has two modes: active and passive.
3. Python and FTP
from ftplib import FTPf = FTP('some.ftp.server')f.login('anonymous','your@email.address') :f.quit()
FTP object Method
| Method |
Description |
| Login (user = '', passwd = '') |
Log on to the FTP server. All parameters are optional. |
| Cwd (path) |
Set the current working directory to the path shown in path |
| Dir ([path [,... [, cb]) |
Display the content in the path directory. The optional parameter cb is a callback function that is passed to the retrlines () method. |
| Pwd () |
Get current working directory |
| Storlines (cmd, f) |
Specifies an FTP command to upload text. To specify a file object f |
| Storbinary () |
Similar to the above, used to process binary files |
| Retrlines (cmd [, cb]) |
A given FTP command is used to download text files. The optional callback function cb is used to process each row in the file. |
| Retrbinary |
Processing binary files |
| Quit () |
Close the connection and exit |