- Inheritance
- < Object
- Included Modules
- MonitorMixin
This class implements the File Transfer Protocol. If you have used a command-line FTP program, and are familiar with the commands, you will be able to use this class easily. Some extra features are included to take advantage of Ruby‘s style and strengths.
Example
require 'net/ftp'
Example 1
ftp = Net::FTP.new('ftp.netlab.co.jp') ftp.login files = ftp.chdir('pub/lang/ruby/contrib') files = ftp.list('n*') ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024) ftp.close
Example 2
Net::FTP.open('ftp.netlab.co.jp') do |ftp| ftp.login files = ftp.chdir('pub/lang/ruby/contrib') files = ftp.list('n*') ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024) end
Major Methods
The following are the methods most likely to be useful to users:
Attributes
Name | Visibility | R/W | Description |
---|---|---|---|
binary | public | RW | When true, transfers are performed in binary mode. Default: true. |
debug_mode | public | RW | When true, all traffic to and from the server is written to +$stdout+. Default: false. |
last_response | public | R | The server‘s last response. |
last_response_code | public | R | The server‘s last response code. |
passive | public | RW | When true, the connection is in passive mode. Default: false. |
resume | public | RW | Sets or retrieves the resume status, which decides whether incomplete transfers are resumed or restarted. Default: false. |
welcome | public | R | The server‘s welcome message. |
Aliases
Method | Alias | Description |
---|---|---|
last_response_code | → lastresp |
Methods
Class
Visibility | Signature |
---|---|
public | new (host = nil, user = nil, passwd = nil, acct = nil) |
public | open (host, user = nil, passwd = nil, acct = nil) {|ftp| ...} |
Instance
Visibility | Signature |
---|---|
public | abort () |
public | acct (account) |
public | chdir (dirname) |
public | close () |
public | closed? () |
public | connect (host, port = FTP_PORT) |
public | delete (filename) |
public | dir (*args) |
public | get (remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...} |
public | getbinaryfile (remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...} |
public | getdir () |
public | gettextfile (remotefile, localfile = File.basename(remotefile)) {|line| ...} |
public | help (arg = nil) |
public | list (*args) {|line| ...} |
public | login (user = "anonymous", passwd = nil, acct = nil) |
public | ls (*args) |
public | mdtm (filename) |
public | mkdir (dirname) |
public | mtime (filename, local = false) |
public | nlst (dir = nil) |
public | noop () |
public | put (localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) |
public | putbinaryfile (localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...} |
public | puttextfile (localfile, remotefile = File.basename(localfile)) {|line| ...} |
public | pwd () |
public | quit () |
public | rename (fromname, toname) |
public | retrbinary (cmd, blocksize, rest_offset = nil) {|data| ...} |
public | retrlines (cmd) {|line| ...} |
public | return_code () |
public | return_code= (s) |
public | rmdir (dirname) |
public | sendcmd (cmd) |
public | set_socket (sock, get_greeting = true) |
public | site (arg) |
public | size (filename) |
public | status () |
public | storbinary (cmd, file, blocksize, rest_offset = nil) {|data| ...} |
public | storlines (cmd, file) {|line| ...} |
public | system () |
public | voidcmd (cmd) |
Class Method Detail
new(host = nil, user = nil, passwd = nil, acct = nil)
Creates and returns a new FTP object. If a host is given, a connection is made. Additionally, if the user is given, the given user name, password, and (optionally) account are used to log in. See login.
open(host, user = nil, passwd = nil, acct = nil) {|ftp| ...}
A synonym for FTP.new, but with a mandatory host parameter.
If a block is given, it is passed the FTP object, which will be closed when the block finishes, or when an exception is raised.
Instance Method Detail
abort()
Aborts the previous command (ABOR command).
acct(account)
Sends the ACCT command. TODO: more info.
chdir(dirname)
Changes the (remote) directory.
close()
Closes the connection. Further operations are impossible until you open a new connection with connect.
closed?()
Returns true iff the connection is closed.
connect(host, port = FTP_PORT)
Establishes an FTP connection to host, optionally overriding the default port. If the environment variable SOCKS_SERVER is set, sets up the connection through a SOCKS proxy. Raises an exception (typically Errno::ECONNREFUSED) if the connection cannot be established.
delete(filename)
Deletes a file on the server.
dir(*args)
Alias for list
get(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...}
Retrieves remotefile in whatever mode the session is set (text or binary). See gettextfile and getbinaryfile.
getbinaryfile(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...}
Retrieves remotefile in binary mode, storing the result in localfile. If a block is supplied, it is passed the retrieved data in blocksize chunks.
getdir()
Alias for pwd
gettextfile(remotefile, localfile = File.basename(remotefile)) {|line| ...}
Retrieves remotefile in ASCII (text) mode, storing the result in localfile. If a block is supplied, it is passed the retrieved data one line at a time.
help(arg = nil)
Issues the HELP command.
list(*args) {|line| ...}
Returns an array of file information in the directory (the output is like `ls -l`). If a block is given, it iterates through the listing.
login(user = "anonymous", passwd = nil, acct = nil)
Logs in to the remote host. The session must have been previously connected. If user is the string "anonymous" and the password is nil, a password of user@host is synthesized. If the acct parameter is not nil, an FTP ACCT command is sent following the successful login. Raises an exception on error (typically Net::FTPPermError).
ls(*args)
Alias for list
mdtm(filename)
Issues the MDTM command. TODO: more info.
mkdir(dirname)
Creates a remote directory.
mtime(filename, local = false)
Returns the last modification time of the (remote) file. If local is true, it is returned as a local time, otherwise it‘s a UTC time.
nlst(dir = nil)
Returns an array of filenames in the remote directory.
noop()
Issues a NOOP command.
put(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block)
Transfers localfile to the server in whatever mode the session is set (text or binary). See puttextfile and putbinaryfile.
putbinaryfile(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE) {|data| ...}
Transfers localfile to the server in binary mode, storing the result in remotefile. If a block is supplied, calls it, passing in the transmitted data in blocksize chunks.
puttextfile(localfile, remotefile = File.basename(localfile)) {|line| ...}
Transfers localfile to the server in ASCII (text) mode, storing the result in remotefile. If callback or an associated block is supplied, calls it, passing in the transmitted data one line at a time.
pwd()
Returns the current remote directory.
quit()
Exits the FTP session.
rename(fromname, toname)
Renames a file on the server.
retrbinary(cmd, blocksize, rest_offset = nil) {|data| ...}
Puts the connection into binary (image) mode, issues the given command, and fetches the data returned, passing it to the associated block in chunks of blocksize characters. Note that cmd is a server command (such as "RETR myfile").
retrlines(cmd) {|line| ...}
Puts the connection into ASCII (text) mode, issues the given command, and passes the resulting data, one line at a time, to the associated block. If no block is given, prints the lines. Note that cmd is a server command (such as "RETR myfile").
return_code()
Obsolete
return_code=(s)
Obsolete
rmdir(dirname)
Removes a remote directory.
sendcmd(cmd)
Sends a command and returns the response.
set_socket(sock, get_greeting = true)
WRITEME or make private
site(arg)
Issues a SITE command.
size(filename)
Returns the size of the given (remote) filename.
status()
Returns the status (STAT command).
storbinary(cmd, file, blocksize, rest_offset = nil) {|data| ...}
Puts the connection into binary (image) mode, issues the given server-side command (such as "STOR myfile"), and sends the contents of the file named file to the server. If the optional block is given, it also passes it the data, in chunks of blocksize characters.
storlines(cmd, file) {|line| ...}
Puts the connection into ASCII (text) mode, issues the given server-side command (such as "STOR myfile"), and sends the contents of the file named file to the server, one line at a time. If the optional block is given, it also passes it the lines.
system()
Returns system information.
voidcmd(cmd)
Sends a command and expect a response beginning with ‘2’.