gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:38:25 28 4
gpt4 key购买 nike

我正在尝试解析一个 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 进行编码:

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

关于ruby - 如何使用具有特殊字符的用户名或密码解析 FTP URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8780200/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com