2016-01-21 63 views
1

当我尝试解析以下网址,为港口URI.parse返回nilURI.parse返回nil端口SFTP和FTPS

require 'uri' 

url = "ftp://example.com" 
puts URI.parse(url).port 
# => 21 

url = "sftp://example.com" 
puts URI.parse(url).port 
# => nil 

url = "ftps://example.com" 
puts URI.parse(url).port 
# => nil 

有人可以解释为什么这个工程的ftphttphttps但不为sftpftps

回答

2

因为sftp和ftps没有分离,所以他们使用的端口也没有。

你可以在URI的文档中检查这个。看看命名空间:

http://ruby-doc.org/stdlib-2.1.0/libdoc/uri/rdoc/URI.html

你会发现FTP,HTTP,HTTPS没有SFTP或FTPS

无法识别的URI的喜欢这些都会型 - >URI ::一般

通过观察他们的文档,你会发现它们有零:

http://ruby-doc.org/stdlib-2.1.0/libdoc/uri/rdoc/URI/Generic.html

默认端口

你可以试试这个:

puts URI.parse("sftp://example.com").class 
1

他们不支持默认情况下,红宝石编 -

p URI.scheme_list 

#=> {"FTP"=>URI::FTP, 
    "HTTP"=>URI::HTTP, 
    "HTTPS"=>URI::HTTPS, 
    "LDAP"=>URI::LDAP, 
    "LDAPS"=>URI::LDAPS, 
    "MAILTO"=>URI::MailTo} 

sftp的和ftps不受任何Ruby类表示。因此,它们的默认端口不会被自动选取。