ftplib
 

An FTP client class, and some helper functions.
Based on RFC 959: File Transfer Protocol
(FTP), by J. Postel and J. Reynolds

Changes and improvements suggested by Steve Majewski.
Modified by Jack to work on the mac.
Modified by Siebren to support docstrings and PASV.


Example:

>>> from ftplib import FTP
>>> ftp = FTP('ftp.python.org') # connect to host, default port
>>> ftp.login() # default, i.e.: user anonymous, passwd user@hostname
'230 Guest login ok, access restrictions apply.'
>>> ftp.retrlines('LIST') # list directory contents
total 9
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
'226 Transfer complete.'
>>> ftp.quit()
'221 Goodbye.'
>>> 

A nice test that reveals some of the network dialogue would be:
python ftplib.py -d localhost -l -p -l


 Modules
                                                                                                                                                                                                                               
os
socket
string
sys


 Classes
                                                                                                                                                                                                                               
FTP
Netrc


 class FTP
           An FTP client class.

To create a connection, call the class using these argument:
        host, user, passwd, acct
These are all strings, and have default value ''.
Then use self.connect() with optional host and port argument.

To download a file, use ftp.retrlines('RETR ' + filename),
or ftp.retrbinary() with slightly different arguments.
To upload a file, use ftp.storlines() or ftp.storbinary(),
which have an open file as argument (see their definitions
below for details).
The download/upload functions first issue appropriate TYPE
and PORT or PASV commands.
 
                                                                                                                                                                                                                     
__init__(self, host='', user='', passwd='', acct='')
# Initialization method (called by class instantiation).
# Initialize host to localhost, port to standard ftp port
# Optional arguments are host (for connect()),
# and user, passwd, acct (for login())
abort(self)
Abort a file transfer.  Uses out-of-band data.
This does not follow the procedure from the RFC to send Telnet
IP and Synch; that doesn't seem to work with the servers I've
tried.  Instead, just send the ABOR command as OOB data.
acct(self, password)
Send new account name.
close(self)
Close the connection without assuming anything about it.
connect(self, host='', port=0)
Connect to host.  Arguments are:
- host: hostname to connect to (string, default previous host)
- port: port to connect to (integer, default previous port)
cwd(self, dirname)
Change to a directory.
set_debuglevel(self, level)
Set the debugging level.
The required argument level means:
0: no debugging output (default)
1: print commands and responses but not body text etc.
2: also print raw lines read and sent before stripping CR/LF
delete(self, filename)
Delete a file.
dir(self, *args)
List a directory in long form.
By default list current directory to stdout.
Optional last argument is callback function; all
non-empty arguments before it are concatenated to the
LIST command.  (This *should* only be used for a pathname.)
getline(self)
# Internal: return one line from the server, stripping CRLF.
# Raise EOFError if the connection is closed
getmultiline(self)
# Internal: get a response from the server, which may possibly
# consist of multiple lines.  Return a single string with no
# trailing CRLF.  If the response consists of multiple lines,
# these are separated by '\n' characters in the string
getresp(self)
# Internal: get a response from the server.
# Raise various errors if the response indicates an error
getwelcome(self)
Get the welcome message from the server.
(this is read and squirreled away by connect())
login(self, user='', passwd='', acct='')
Login, default anonymous.
makeport(self)
Create a new socket and send a PORT command for it.
mkd(self, dirname)
Make a directory, return its full pathname.
nlst(self, *args)
Return a list of files in a given directory (default the current).
ntransfercmd(self, cmd)
Initiate a transfer over the data connection.
If the transfer is active, send a port command and
the transfer command, and accept the connection.
If the server is passive, send a pasv command, connect
to it, and start the transfer command.
Either way, return the socket for the connection and
the expected size of the transfer.  The expected size
may be None if it could not be determined.
putcmd(self, line)
# Internal: send one command to the server (through putline())
putline(self, line)
# Internal: send one line to the server, appending CRLF
pwd(self)
Return current working directory.
quit(self)
Quit, and close the connection.
rename(self, fromname, toname)
Rename a file.
retrbinary(self, cmd, callback, blocksize=8192)
Retrieve data in binary mode.
The argument is a RETR command.
The callback function is called for each block.
This creates a new port for you
retrlines(self, cmd, callback=None)
Retrieve data in line mode.
The argument is a RETR or LIST command.
The callback function (2nd argument) is called for each line,
with trailing CRLF stripped.  This creates a new port for you.
print_lines is the default callback.
rmd(self, dirname)
Remove a directory.
sanitize(self, s)
# Internal: "sanitize" a string for printing
sendcmd(self, cmd)
Send a command and return the response.
sendport(self, host, port)
Send a PORT command with the current host and the given port number.
set_debuglevel(self, level)
Set the debugging level.
The required argument level means:
0: no debugging output (default)
1: print commands and responses but not body text etc.
2: also print raw lines read and sent before stripping CR/LF
set_pasv(self, val)
Use passive or active mode for data transfers.
With a false argument, use the normal PORT mode,
With a true argument, use the PASV command.
size(self, filename)
Retrieve the size of a file.
storbinary(self, cmd, fp, blocksize)
Store a file in binary mode.
storlines(self, cmd, fp)
Store a file in line mode.
transfercmd(self, cmd)
Initiate a transfer over the data connection.  Returns
the socket for the connection.  See also ntransfercmd().
voidcmd(self, cmd)
Send a command and expect a response beginning with '2'.
voidresp(self)
Expect a response beginning with '2'.


 class Netrc
           Class to parse & provide access to 'netrc' format files.

See the netrc(4) man page for information on the file format.

WARNING: This class is obsolete -- use module netrc instead.
 
                                                                                                                                                                                                                     
__init__(self, filename=None)
no doc string
get_account(self, host)
Returns login information for the named host.
The return value is a triple containing userid,
password, and the accounting field.
get_hosts(self)
Return a list of hosts mentioned in the .netrc file.
get_macro(self, macro)
Return a sequence of lines which define a named macro.
get_macros(self)
Return a list of all defined macro names.


 Functions
                                                                                                                                                                                                                               
ftpcp(source, sourcename, target, targetname='', type='I')
Copy file from one FTP-instance to another.
parse150(resp)
Parse the '150' response for a RETR request.
Returns the expected transfer size or None; size is not guaranteed to
be present in the 150 message.
parse227(resp)
Parse the '227' response for a PASV request.
Raises error_proto if it does not contain '(h1,h2,h3,h4,p1,p2)'
Return ('host.addr.as.numbers', port#) tuple.
parse257(resp)
Parse the '257' response for a MKD or PWD request.
This is a response to a MKD or PWD request: a directory name.
Returns the directoryname in the 257 reply.
print_line(line)
Default retrlines callback to print a line.
test()
Test program.
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...