How can I parse an FTP URL with a username or password that has special characters?

I am trying to parse an FTP URL that has special characters like @ in username and password:

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

When I try:

 URI.parse(url) 

I get:

URI :: InvalidURIError: the ftp scheme does not accept part of the registry: username: p @sswrd @ ftp.myhost.com (or an incorrect host name?)

Then I tried to encode the URL:

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

But another error also appeared:

URI :: InvalidURIError: ftp scheme does not accept part of the registry: username: p% 40sswrd% 40ftp.myhost.com (or an incorrect host name?)

Finally, I tried another solution:

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

But I also got an error message:

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

I am using ruby ​​1.8.7.

+4
source share
3 answers
 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 
+1
source

If your ftp server supports unicode:

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

must work. As indicated by this discussion .

But I just realized that you tried coding, and it failed. Unfortunately.

0
source

I needed to set passive mode as I was getting the following error:

 425 Could not open data connection to port XXXXX: Connection timed out 

I did it like this:

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

Source: https://habr.com/ru/post/1389926/


All Articles