[英]Using Net::FTP instance method `open` for FTP with custom port
I have a remote ftp server. 我有一个远程ftp服务器。
I can connect to it with CyberDuck or terminal ftp client. 我可以使用CyberDuck或终端ftp客户端连接到它。
When trying to connect with Net::FTP
I can do this: 当尝试连接Net::FTP
我可以这样做:
ftp=Net::FTP.new
ftp.connect('url', 'port')
ftp.login('username', 'password')
However, doing this: 但是,这样做:
Net::FTP.open('url', 'username', 'password')
returns Errno::ECONNREFUSED: Connection refused - connect(2)
. 返回Errno::ECONNREFUSED: Connection refused - connect(2)
。
It seems like .open
doesn't accept a port parameter, resulting in an error for me since I use custom port. 似乎.open
不接受端口参数,因为我使用自定义端口导致错误。 But I need to use .open
method, because it's used in external gem I use ( carrierwave-webdav
). 但我需要使用.open
方法,因为它在我使用的外部gem中使用( carrierwave-webdav
)。
Any workaround for this one or should I create my own fork? 这个的任何解决方法或我应该创建自己的分叉?
How can I connect to my ftp using .open
method? 如何使用.open
方法连接到我的ftp?
If you pass
host argument to
Net::FTP.new
or mandatory to
Net::FTP.open
it tries to connect immediately so you have to pass non-standard port number in other way like passing together with a hostname:
如果您将
主机参数传递给
Net::FTP.new
或强制传递给
Net::FTP.open
它会立即尝试连接,因此您必须以其他方式传递非标准端口号,例如与主机名一起传递:
Net::FTP.open('url:port', 'username', 'password')
Net :: FTP.open('url:port','username','password')
UPDATE: Ruby net library unfortunately doesn't parse host string so you need perform all connection steps separately: 更新:遗憾的是,Ruby net库不解析主机字符串,因此您需要单独执行所有连接步骤:
ftp = Net::FTP.new # don't pass hostname or it will try open on default port
ftp.connect('url', port) # here you can pass a non-standard port number
ftp.login('username', 'password')
ftp.passive = true # optional, if PASV mode is required
The @jackbot's answer is correct, and sample code for setting FTP_PORT
is very simple: @ jackbot的答案是正确的,设置FTP_PORT
示例代码非常简单:
Net::FTP.send(:remove_const, 'FTP_PORT') # just to avoid warnings
Net::FTP.const_set('FTP_PORT', 22222)
where 22222 is new ftp port. 其中22222是新的ftp端口。
The full chain is: open
calls new
, which calls connect
, which uses FTP_PORT
. 完整链是: open
调用new
,调用connect
,使用FTP_PORT
。 You can look at source code here: http://docs.ruby-lang.org/en/2.0.0/Net/FTP.html#method-c-new . 你可以在这里查看源代码: http : //docs.ruby-lang.org/en/2.0.0/Net/FTP.html#method-c-new 。
It seems Net::FTP::open
calls Net::FTP::connect
under the hood, which connects to a port set in the FTP_PORT
constant . 似乎Net::FTP::open
调用Net::FTP::connect
,它连接到FTP_PORT
常量中设置的端口。 It's not a very nice solution but you could set that constant to the port you need. 这不是一个非常好的解决方案,但您可以将该常量设置为您需要的端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.