Module ftplib :: Class FTP
[hide private]
[frames] | no frames]

_ClassType 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.

Instance Methods [hide private]
 
__init__(self, host='', user='', passwd='', acct='')
 
connect(self, host='', port=0)
Connect to host.
 
getwelcome(self)
Get the welcome message from the server.
 
set_debuglevel(self, level)
Set the debugging level.
 
debug(self, level)
Set the debugging level.
 
set_pasv(self, val)
Use passive or active mode for data transfers.
 
sanitize(self, s)
 
putline(self, line)
 
putcmd(self, line)
 
getline(self)
 
getmultiline(self)
 
getresp(self)
 
voidresp(self)
Expect a response beginning with '2'.
 
abort(self)
Abort a file transfer.
 
sendcmd(self, cmd)
Send a command and return the response.
 
voidcmd(self, cmd)
Send a command and expect a response beginning with '2'.
 
sendport(self, host, port)
Send a PORT command with the current host and the given port number.
 
sendeprt(self, host, port)
Send a EPRT command with the current host and the given port number.
 
makeport(self)
Create a new socket and send a PORT command for it.
 
makepasv(self)
 
ntransfercmd(self, cmd, rest=None)
Initiate a transfer over the data connection.
 
transfercmd(self, cmd, rest=None)
Like ntransfercmd() but returns only the socket.
 
login(self, user='', passwd='', acct='')
Login, default anonymous.
 
retrbinary(self, cmd, callback, blocksize=8192, rest=None)
Retrieve data in binary mode.
 
retrlines(self, cmd, callback=None)
Retrieve data in line mode.
 
storbinary(self, cmd, fp, blocksize=8192)
Store a file in binary mode.
 
storlines(self, cmd, fp)
Store a file in line mode.
 
acct(self, password)
Send new account name.
 
nlst(self, *args)
Return a list of files in a given directory (default the current).
 
dir(self, *args)
List a directory in long form.
 
rename(self, fromname, toname)
Rename a file.
 
delete(self, filename)
Delete a file.
 
cwd(self, dirname)
Change to a directory.
 
size(self, filename)
Retrieve the size of a file.
 
mkd(self, dirname)
Make a directory, return its full pathname.
 
rmd(self, dirname)
Remove a directory.
 
pwd(self)
Return current working directory.
 
quit(self)
Quit, and close the connection.
 
close(self)
Close the connection without assuming anything about it.
Class Variables [hide private]
  debugging = 0
  host = ''
  port = 21
  sock = None
  file = None
  welcome = None
  passiveserver = 1
Method Details [hide private]

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)

getwelcome(self)

 

Get the welcome message from the server. (this is read and squirreled away by connect())

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

debug(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.

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.

ntransfercmd(self, cmd, rest=None)

 

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.

Optional `rest' argument can be a string that is sent as the argument to a RESTART command. This is essentially a server marker used to tell the server to skip over any data up to the given marker.

retrbinary(self, cmd, callback, blocksize=8192, rest=None)

 

Retrieve data in binary mode.

`cmd' is a RETR command. `callback' is a callback function is called for each block. No more than `blocksize' number of bytes will be read from the socket. Optional `rest' is passed to transfercmd().

A new port is created for you. Return the response code.

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_line() is the default callback.

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.)