为什么 Net::FTP 无法连接到服务器? [英] Why can't Net::FTP connect to server?

查看:73
本文介绍了为什么 Net::FTP 无法连接到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,以使用 Ruby 从 FTP 服务器列出和下载数据.我是 Ruby 的新手,所以我查找了如何使用 Net::FTP 的文档.我无法理解为什么这不起作用:

需要'net/ftp'server = "ftp.server.com"用户 = "我的用户"密码 = "我的密码"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

如果你很好奇,下面是区别(来自维基百科)主动和被动模式.

  1. 主动模式下,客户端创建到服务器的TCP控制连接,并将客户端的IP地址和任意客户端端口号发送给服务器,然后等待服务器通过TCP 到该客户端 IP 地址和客户端端口号.在客户端位于防火墙后面并且无法接受传入 TCP 连接的情况下,可以使用被动模式.
  2. 被动模式下,客户端使用控制连接向服务器发送 PASV 命令,然后从服务器接收服务器 IP 地址和服务器端口号,然后客户端使用它们打开一个从任意客户端端口到收到的服务器 IP 地址和服务器端口号的数据连接.

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.com"
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
end

That 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.close

And if you're curious, following is the difference (from wikipedia) between active and passive modes.

  1. In Active mode, the client creates a TCP control connection to the server and sends the server the client's IP address and an arbitrary client port number, and then waits until the server initiates the data connection over TCP to that client IP address and client port number. In situations where the client is behind a firewall and unable to accept incoming TCP connections, passive mode may be used.
  2. In Passive mode, the client uses the control connection to send a PASV command to the server and then receives a server IP address and server port number from the server which the client then uses to open a data connection from an arbitrary client port to the server IP address and server port number received.

这篇关于为什么 Net::FTP 无法连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆