FTP file upload with custom port number in Rails

Its quite weird that I’ve never come across a scenario task that needed the involvement of FTP, at least as far as Rails is concerned. In this case, it involved a FTP PUSH whereby the application was to upload a file to a remote FTP Server. Its pretty simple:

ftp=Net::FTP.new
 ftp.passive = true
 ftp.connect("URL","PORT")
 ftp.login("USERNAME", "PASSWORD")
 ftp.putbinaryfile("/PATH/TO/FILE")
 ftp.close

In most cases, the port number defaults to 21 and hence you can use the open method instead of connect:

Net::FTP.open('URL') do |ftp|
 ftp.passive = true
 ftp.login('USERNAME', 'PASSWORD')
 ftp.putbinaryfile("/PATH/TO/FILE")

end

But in my case, the FTP server had a different command port hence had to use the connect method.  The other thing that’s worth mentioning is the passive flag. Rails sends files via FTP in active mode by default. In active mode, the server initiates a connection from its command port to the client through a random port (in the client). This method has issues because firewalls block the TCP connection from the server to the client.

In passive mode however, the client initiates both connections to the server, solving the problem of firewalls filtering the incoming data port connection to the client from the server.

Cheers!

Did this waste your time or did you benefit from it? Let me know :-) Feel free to add your own views too