我正在尝试创建一个脚本,以使用 Ruby 从 FTP 服务器列出和下载数据.我是 Ruby 的新手,所以我查找了如何使用 Net::FTP 的文档.我无法理解为什么这不起作用:
需要'net/ftp'server = "ftp.server"用户 = "我的用户"密码 = "我的密码"Net::FTP.open(server, user, password) do |ftp|文件 = ftp.chdir('我的目录/')文件 = ftp.list将列表放在目录外:"放置文件结尾这不起作用,返回此错误:
/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:298:in `getresp': 425 >无法建立连接.(网络::FTPTempError)来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:325:in `block in sendcmd'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:323:in `sendcmd'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:402:in `transfercmd'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:478:in `block (2 levels) in retrlines'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:178:in `with_binary'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:477:in `block in retrlines'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:476:in `retrlines'来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:722:in `list'来自 test_ftp.rb:10:in `block in '来自/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:116:in `open'来自 test_ftp.rb:8:in `'谁能解释一下我的脚本有什么问题?
解决方案你的代码很适合我.我怀疑问题可能是因为 Net::FTP 连接模式,默认情况下处于活动状态.尝试使用被动模式连接,以下代码示例 -
ftp = Net::FTP.new(server)ftp.passive = 真ftp.login 用户、密码文件 = ftp.chdir('我的目录/')文件 = ftp.list将列表放在目录外:"放置文件ftp.close如果你很好奇,下面是区别(来自维基百科)主动和被动模式.
I am trying to create a script to list and download data from a FTP server with Ruby. I am new to Ruby so I looked for documentation how to use Net::FTP. I have trouble understanding why this doesn't work:
require 'net/ftp' server = "ftp.server" user = "myuser" password = "mypassword" Net::FTP.open(server, user, password) do |ftp| files = ftp.chdir('mydirectory/') files = ftp.list puts "list out of directory:" puts files endThat doesn't work, returning this error:
/home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:298:in `getresp': 425 >Failed to establish connection. (Net::FTPTempError) from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:325:in `block in sendcmd' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:323:in `sendcmd' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:402:in `transfercmd' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:478:in `block (2 levels) in retrlines' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:178:in `with_binary' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:477:in `block in retrlines' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:476:in `retrlines' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:722:in `list' from test_ftp.rb:10:in `block in ' from /home/adhown/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/ftp.rb:116:in `open' from test_ftp.rb:8:in `'Can anyone explain what's wrong with my script?
解决方案Your code works fine for me. I suspect problem could be because of Net::FTP connection mode, which is by default active. Try connecting using passive mode, following code sample -
ftp = Net::FTP.new(server) ftp.passive = true ftp.login user, password files = ftp.chdir('mydirectory/') files = ftp.list puts "list out of directory:" puts files ftp.closeAnd if you're curious, following is the difference (from wikipedia) between active and passive modes.
更多推荐
为什么 Net::FTP 无法连接到服务器?
发布评论