Ruby/Network/FTP Client

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

change to any directory in the remote filesystem:

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
ftp.chdir("pub")
ftp.chdir("/pub/ruby")
ftp.list("*") { |file| puts file }
ftp.close



Connecting to an FTP server with net/ftp using an FTP URL is a simple operation:

require "net/ftp"
require "uri"
uri = URI.parse("ftp://ftp.ruby-lang.org/")
Net::FTP.open(uri.host) do |ftp|
  ftp.login "anonymous", "me@privacy.net"
  ftp.passive = true
  ftp.list(uri.path) { |path| puts path }
end



Create a temporary file with Tempfile and upload from that

require "net/ftp"
require "tempfile"
tempfile = Tempfile.new("test")
my_data = "This is some text data I want to upload via FTP."
tempfile.puts my_data
ftp = Net::FTP.new("ftp.domain.ru")
ftp.passive = true
ftp.login
ftp.chdir("/your/folder/name/here")
ftp.puttextfile(tempfile.path, "my_data")
ftp.close
tempfile.close



delete and rename files

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
 
ftp.rename(filename, new_name)
 
ftp.list("*") { |file| puts file }
ftp.close



Downloading Files

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login
ftp.chdir("/pub/ruby/1.8")
ftp.getbinaryfile("1.8.2-patch1.gz")
ftp.close



File Transfers with FTP

require "open-uri"
output = File.new("1.8.2-patch1.gz", "w")
open("ftp://ftp.ruby-lang.org/pub/ruby/1.8/1.8.2-patch1.gz") do |f|
  output.print f.read
end
output.close



Ftp mkdir

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
ftp.mkdir("test")
ftp.list("*") { |file| puts file }
ftp.close



if a username and password are required, use this code:

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
ftp.list("*") { |file| puts file }
ftp.close



keep the user informed of the progress of the upload.

require "net/ftp"
ftp = Net::FTP.new("ftp.domain.ru")
ftp.passive = true
ftp.login
ftp.chdir("/your/folder/name/here")
count = 0
ftp.putbinaryfile("local_file", "local_file", 100000) do |block|
  count += 100000
  puts "#{count} bytes uploaded"
end
ftp.close



Net::FTP provides a login method that you can use against a Net::FTP object, like so:

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login
ftp.list("*") { |file| puts file }
ftp.close



prints a string to the screen whenever another 100 kilobytes of the file have been downloaded.

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login
ftp.chdir("/pub/ruby/1.8")
ftp.getbinaryfile("stable-snapshot.tar.gz", "local-filename", 102400) do |blk|
  puts "A 100KB block of the file has been downloaded"
end
ftp.close



Set the mode to passive

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
ftp.delete(filename)
ftp.list("*") { |file| puts file }
ftp.close



Uploading Files

require "net/ftp"
ftp = Net::FTP.new("ftp.domain.ru")
ftp.passive = true
ftp.login
ftp.chdir("/your/folder/name/here")
ftp.putbinaryfile("local_file")
ftp.close



use the chdir method:

require "net/ftp"
ftp = Net::FTP.new("ftp.ruby-lang.org")
ftp.passive = true
ftp.login(username, password)
ftp.chdir("pub")
ftp.list("*") { |file| puts file }
ftp.close