Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a FTP URL with the username or password having special characters?

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

username:p@[email protected]/mypath

When I try:

URI.parse(url)

I get:

URI::InvalidURIError: the scheme ftp does not accept registry part: username:p@[email protected] (or bad hostname?)

Then, I tried to encode the url:

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

But also got another error:

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

Finally, I tried another solution:

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

But I also got an error:

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

I am using ruby 1.8.7.

like image 225
Mahmoud Khaled Avatar asked Nov 14 '22 12:11

Mahmoud Khaled


1 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
like image 97
Mahmoud Khaled Avatar answered May 10 '23 19:05

Mahmoud Khaled