Introducing the ftpd gem

# gem install ftpd

require 'ftpd'
require 'tmpdir'

class Driver

  def initialize(temp_dir)
    @temp_dir = temp_dir
  end

  def authenticate(user, password)
    true
  end

  def file_system(user)
    Ftpd::DiskFileSystem.new(@temp_dir)
  end

end

Dir.mktmpdir do |temp_dir|
  driver = Driver.new(temp_dir)
  server = Ftpd::FtpServer.new(driver)
  server.start
  puts "Server listening on port #{server.bound_port}"
  gets
end

#=> port: 35219

After running the above script, I logged into the localhost FTP server on port 35219 and uploaded a file. I then observed that file in a directory within the temp directory.

Note: If you want to run the server on port 21 you need to be logged in as root and then simply set the server port using server.port = 21 before starting the server.

Resources

Tags:
Source:
1233hrs.txt
Published:
17-02-2015 12:33