3

I have a remote ftp server.
I can connect to it with CyberDuck or terminal ftp client.

When trying to connect with Net::FTP I can do this:

ftp=Net::FTP.new
ftp.connect('url', 'port')
ftp.login('username', 'password')

However, doing this:

Net::FTP.open('url', 'username', 'password')

returns Errno::ECONNREFUSED: Connection refused - connect(2).

It seems like .open doesn't accept a port parameter, resulting in an error for me since I use custom port. But I need to use .open method, because it's used in external gem I use (carrierwave-webdav).
Any workaround for this one or should I create my own fork?
How can I connect to my ftp using .open method?

konnigun
  • 1,797
  • 2
  • 17
  • 30

3 Answers3

4

If you pass host argument to Net::FTP.new or mandatory to Net::FTP.open it tries to connect immediately so you have to pass non-standard port number in other way like passing together with a hostname:

Net::FTP.open('url:port', 'username', 'password')

UPDATE: Ruby net library unfortunately doesn't parse host string so you need perform all connection steps separately:

ftp = Net::FTP.new  # don't pass hostname or it will try open on default port
ftp.connect('url', port)  # here you can pass a non-standard port number
ftp.login('username', 'password')
ftp.passive = true  # optional, if PASV mode is required
David Unric
  • 7,421
  • 1
  • 37
  • 65
2

The @jackbot's answer is correct, and sample code for setting FTP_PORT is very simple:

Net::FTP.send(:remove_const, 'FTP_PORT') # just to avoid warnings
Net::FTP.const_set('FTP_PORT', 22222)

where 22222 is new ftp port.

The full chain is: open calls new, which calls connect, which uses FTP_PORT. You can look at source code here: http://docs.ruby-lang.org/en/2.0.0/Net/FTP.html#method-c-new.

denis.peplin
  • 9,585
  • 3
  • 48
  • 55
1

It seems Net::FTP::open calls Net::FTP::connect under the hood, which connects to a port set in the FTP_PORT constant. It's not a very nice solution but you could set that constant to the port you need.

jackbot
  • 2,931
  • 3
  • 27
  • 35