The most commonly used methods are listed in Table 1, which is not comprehensive--to see all the methods, see Module source code
--but the methods listed here make up our "API" for FTP client programming in Python.
That is, you do not have to use other methods, because they are either auxiliary functions, or administrative functions, or
Called by the API.
Method description
Login (user= ' anonymous ', login to FTP server, all parameters are optional
Passwd= ", acct=")
PWD () Gets the current working directory
CWD (path) sets the current working directory to Path
Dir ([path[,... [, CB]]) Displays the contents of the path directory, the optional parameter CB is a callback function that
will be passed to the Retrlines () method
Nlst ([path[,...]) Similar to Dir (), but returns a list of file names instead of displaying these filenames
Retrlines (cmd [, CB]) given an FTP command (such as "RETR filename") for downloading a text file. Optional callback function CB is used to process each line of the file
Retrbinary (cmd, cb[,
bs=8192[, RA]]) is similar to Retrlines (), except that this instruction handles binary files. The callback function CB is used to process each piece of data downloaded (The block size defaults to 8K).
Storlines (cmd, f) is given an FTP command (such as "STOR filename") to upload a text file. To be given a file object F
Storbinary (cmd, f[, and Storlines () are similar, except that this instruction handles binary files. To give a file object F, the upload block size BS defaults to 8kbs=8192])
BS=8192])
Rename (old, new) rename the remote file old to new
Delete (path) deletes the remote file located in path
MKD (directory) to create a remote directory
RMD (directory) Delete remote directory
Quit () Close the connection and exit
In general FTP Communication, the command to be used has login (), CWD (), dir (), PWD (), stor* (), retr* ()
and quit (). It is also useful to have some FTP object methods that are not listed.
The establishment of FTP can be ftp=ftplib first. FTP () is instantiated and then Ftp=ftp.connect (' User ', ' passwd '), or the link ftp=ftplib can be instantiated directly. FTP (' user ', ' passwd ') refer to the Python documentation for more information on
Information for the FTP object:
Http://python.org/docs/current/lib/ftp-objects.html
Example 1 shows how to log in and get a list of files in the login directory. Note that the file column here
Table (column directory operations) format is server-related (general and host Platform Column directory tool output format
such as LS under Unix and dir under Windows/dos) Example 1. Using the Ftplib module to get a list of directories
file:ftplib-example-1.py
Import Ftplib
FTP = Ftplib. FTP ("www.python.org")
Ftp.login ("Anonymous", "Ftplib-example-1")
Print Ftp.dir ()
Ftp.quit ()
Total 34
Drwxrwxr-x
Drwxrwxr-x
Drwxrwxr-x
lrwxrwxrwx
Welcome.msg
Drwxr-xr-x
Drwxr-sr-x
drwxrwxr--
Drwxr-xr-x
...
11
11
2
1 root
Root
Root
Root 4127
4127
4127
Bin 512
512
512
Sep
Sep
Sep
June 14
14
13
29 14:18
14:18
15:18
14:34
3
3
2
3 root
Root
Root
Root Wheel
1400
4127
Wheel 512
512
512
19
June 9
Feb 8
May 19 1998
1997
1998
1998
.
..
Rcs
README
Bin
Dev
Dup
etc
Downloading the file is simple; Use the appropriate RETR function. Note that when you download a text file, you
You must add the line terminator yourself. A lambda expression was used in Example 2 to complete this
Job.
Example 2. Downloading files using the Ftplib module
file:ftplib-example-2.py
Import Ftplib
Import Sys
def gettext (FTP, filename, outfile=none):
# Fetch a text file
If OutFile is None:
outfile = Sys.stdout
# Use a lambda-to-add newlines to the lines read from the server
Ftp.retrlines ("RETR" + filename, lambda s, W=outfile.write:
W (s+ "\ n"))
def getbinary (FTP, filename, outfile=none): # Fetch a binary file
If OutFile is None:
outfile = Sys.stdout
Ftp.retrbinary ("RETR" + filename, outfile.write)
FTP = Ftplib. FTP ("www.python.org")
Ftp.login ("Anonymous", "Ftplib-example-2")
GetText (FTP, "README")
Getbinary (FTP, "welcome.msg")
WELCOME to python.org, the Python programming language home site.
You are number%N of%M allowed users.
ni!
Python Web site:http://www.python.org/
Confused FTP CLIENT? Try begining your login password with '-' dash.
This turns off continuation messages is confusing your client.
...
Finally, Example 3 copies the files to the FTP server. This script uses the file name extension to
Determines whether the file is a text file or a binary file.
Example 3. Uploading files using the Ftplib module
file:ftplib-example-3.py
Import Ftplib
Import OS
def upload (FTP, file):
ext = os.path.splitext (file) [1]
If ext in (". txt", ". htm", ". html"):
Ftp.storlines ("STOR" + file, open (file))
Else
Ftp.storbinary ("STOR" + file, open (file, "RB"), 1024)
FTP = Ftplib. FTP ("ftp.fbi.gov")
Ftp.login ("Mulder", "Trustno1")
Upload (FTP, "Trixie.zip")
Upload (FTP, "file.txt")
Upload (FTP, "sightings.jpg")
Ftplib. FTP class Methods