2016-01-21 55 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類表示。因此,它們的默認端口不會被自動選取。