Last Updated: February 25, 2016
·
1.542K
· nosolopau

Faster FTP get in Ruby

Get faster FTP transfer with FTP lib in Ruby by disabling reverse DNS lookup (it can be very slow):

Use:

BasicSocket.do_not_reverse_lookup = true
ftp = Net::FTP.new('server', 'user', 'password')

Full example:

def download_file(file)
  BasicSocket.do_not_reverse_lookup = true
  ftp = Net::FTP.new('server', 'user', 'password')
  ftp.passive = true

  begin
    logger.info "info: Downloading #{file}."
    ftp.getbinaryfile(File.basename(file), file, 1024)
  rescue Net::FTPPermError => e
    logger.info "warning: can't download #{File.basename(file)} from the remote server (#{e.message.tr("\n","")})."
  end

  ftp.close
end