Ruby net / ftp代码由于错误而无法放入文件 [英] Ruby net/ftp code failing to PUT a file because of error

查看:223
本文介绍了Ruby net / ftp代码由于错误而无法放入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个失败的基本代码:

  ftpconfighash = open(#{RAILS_ROOT} / config / ftp。 yml){| f | YAML.load(f)} 
config = ftpconfighash [12345]

ftp = Net :: FTP :: new(config [host])
ftp.login( config [username],config [password])

dir + =/除非配置[dir]。或者config [dir]。ends_with?(/)
remotename = File.basename(filename)
remotename = dir + remotename,除非dir.blank?

如果File.binary?(文件名)
放置将二进制文件#{文件名}放置到#{remotename}
ftp.putbinaryfile(文件名,远程名称)
else
将将ASCII文件#{文件名}存入#{remotename}
ftp.puttextfile(filename,remotename)
end
ftp.close

我已验证我可以ftp到服务器并手动输入文件。我还使用irb逐行浏览代码。我使用XML文件作为测试,所以ftp.puttextfile是失败的行。这是错误:

  Net :: FTPPermError:500无效的PORT命令。 

我不明白。我已经证实我确实可以放这个文件,但是我不能用这段代码来完成。



我将ftp.debug_mode设置为true,这是输出来自ftp.puttextfile命令:

  put:TYPE A 
get:200类型设置为A.
put:PORT 10,0,1,20,198,170
get:500无效的PORT命令。
Net :: FTPPermError:500无效的PORT命令。

from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:243:in`getresp'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:251:in`voidresp'
from /Users/me/.rvm/rubies /ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:274:in`voidcmd'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/ lib / ruby​​ / 1.8 / monitor.rb:242:在`同步'$ / $ b / from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb :272:在`voidcmd'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:287:in`sendport'$ b $ / from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:295:in`makeport'
from / Users / me /。 rvm / rubies / ruby​​-1.8.7-p352 / lib / ruby​​ / 1.8 / net / ftp.rb:328:在`transfercmd'
from /Users/me/.rvm/rubies/ruby-1.8.7 -p352 / lib / ruby​​ / 1.8 / net / ftp.rb:424:在`retrlines'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/ monitor.rb:242:从/Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ft的`同步'
p.rb:422:在`retrlines'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:631:in`list '
from(irb):42

Ruby似乎试图运行一个命令FTP服务器不理解:

  Port 10,0,1,20,198,170 

有没有办法抑制这个命令,或者我缺少其他的东西?

  ftp = Net :: FTP :: new( config [host])
ftp.login(config [username],config [password])

ftp.passive = true


I have this basic code that is failing:

  ftpconfighash = open("#{RAILS_ROOT}/config/ftp.yml") {|f| YAML.load(f) }
  config = ftpconfighash[12345]

  ftp = Net::FTP::new(config["host"])
  ftp.login(config["username"], config["password"])

  dir += "/" unless config["dir"].blank? or config["dir"].ends_with?("/")
  remotename = File.basename(filename)
  remotename = dir + remotename unless dir.blank?

  if File.binary?(filename)
    puts "PUTting binary file #{filename} to #{remotename}"
    ftp.putbinaryfile(filename, remotename)
  else
    puts "PUTting ASCII file #{filename} to #{remotename}"
    ftp.puttextfile(filename, remotename)
  end
  ftp.close

I have verified that I can ftp to the server and PUT a file by hand. I have also used irb to walk through the code line-by-line. I am using an XML file as a test, so the "ftp.puttextfile" is the line that is failing. Here is the error:

Net::FTPPermError: 500 Invalid PORT Command.

I don't understand. I have verified that I can indeed put this file, but I just can't do it with this code.

I set ftp.debug_mode to true, and this is the output from the ftp.puttextfile command:

put: TYPE A
get: 200 Type set to A.
put: PORT 10,0,1,20,198,170
get: 500 Invalid PORT Command.
Net::FTPPermError: 500 Invalid PORT Command.

from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:243:in `getresp'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:251:in `voidresp'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:274:in `voidcmd'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:272:in `voidcmd'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:287:in `sendport'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:295:in `makeport'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:328:in `transfercmd'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:424:in `retrlines'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:422:in `retrlines'
from /Users/me/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/net/ftp.rb:631:in `list'
from (irb):42

It looks like Ruby is trying to run a command that the FTP server doesn't understand:

PORT 10,0,1,20,198,170

Is there some way to suppress that command, or am I missing something else?

解决方案

Try using passive ftp:

ftp = Net::FTP::new(config["host"])
ftp.login(config["username"], config["password"])

ftp.passive = true

这篇关于Ruby net / ftp代码由于错误而无法放入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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