加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何使用具有特殊字符的用户名或密码解析FTP URL?

发布时间:2020-12-17 02:00:47 所属栏目:百科 来源:网络整理
导读:我正在尝试解析在用户名和密码中包含一些特殊字符(如@)的FTP URL: username:p@sswrd@ftp.myhost.com/mypath 当我尝试: URI.parse(url) 我明白了: URI::InvalidURIError: the scheme ftp does not accept registry part: username:p@sswrd@ftp.myhost.com
我正在尝试解析在用户名和密码中包含一些特殊字符(如@)的FTP URL:

username:p@sswrd@ftp.myhost.com/mypath

当我尝试:

URI.parse(url)

我明白了:

URI::InvalidURIError: the scheme ftp does not accept registry part: username:p@sswrd@ftp.myhost.com (or bad hostname?)

然后,我尝试编码网址:

url = URI.encode(url,'@')

但也有另一个错误:

URI::InvalidURIError: the scheme ftp does not accept registry part: username:p%40sswrd%40ftp.myhost.com (or bad hostname?)

最后,我试了另一个解决方案:

URI::FTP.build(:userinfo => 'username:p@sswrd',:host=>'ftp.myhost.com',:path => '/mypath')

但我也有一个错误:

URI::InvalidComponentError: bad component(expected user component): p@ssword

我使用的是ruby 1.8.7.

解决方法

require 'net/ftp'
ftp=Net::FTP.new
ftp.connect("ftp.myhost.com",21)
ftp.login("username","p@sswd")
ftp.getbinaryfile("/mypath"){|data| puts data}
ftp.close

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读