簡體   English   中英

如何使用 Ruby 2.3 連接到 FTPS?

[英]How to connect to FTPS using Ruby 2.3?

從我看到的支持 FTPS 的最新 Ruby 是 1.8。 我發現了一些可以連接到 FTPS 的 gem,但它們已經好幾年沒有更新了。 最近有人必須這樣做嗎? 你用的什么寶石?

您可以簡單地使用net/ftp標准庫。

ftp = Net::FTP.new('cdimage.debian.org')
ftp.login
ftp.list

或登錄受保護的 ftp:

ftp.login('username', 'password')

對於 FTPS,您可以使用 net/sftp https://github.com/net-ssh/net-sftp

代碼示例:

require 'net/sftp'

Net::SFTP.start('host', 'username', :password => 'password') do |sftp|
  # upload a file or directory to the remote host
  sftp.upload!("/path/to/local", "/path/to/remote")

  # download a file or directory from the remote host
  sftp.download!("/path/to/remote", "/path/to/local")

  # grab data off the remote host directly to a buffer
  data = sftp.download!("/path/to/remote")

  # open and write to a pseudo-IO for a remote file
  sftp.file.open("/path/to/remote", "w") do |f|
    f.puts "Hello, world!\n"
  end

  # open and read from a pseudo-IO for a remote file
  sftp.file.open("/path/to/remote", "r") do |f|
    puts f.gets
  end

  # create a directory
  sftp.mkdir! "/path/to/directory"

  # list the entries in a directory
  sftp.dir.foreach("/path/to/directory") do |entry|
    puts entry.longname
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM