为什么 Net::FTP 无法连接到服务器?

编程入门 行业动态 更新时间:2024-10-25 08:23:19
本文介绍了为什么 Net::FTP 无法连接到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个脚本,以使用 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

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

  • 在主动模式下,客户端创建到服务器的TCP控制连接,并将客户端的IP地址和任意客户端端口号发送给服务器,然后等待服务器通过TCP 到该客户端 IP 地址和客户端端口号.在客户端位于防火墙后面并且无法接受传入 TCP 连接的情况下,可以使用被动模式.
  • 在被动模式下,客户端使用控制连接向服务器发送 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" 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.

  • 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.
  • 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 无法连接到服务器?

    本文发布于:2023-10-15 01:13:48,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1492807.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:连接到   服务器   Net   FTP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!