AmanKing

HTTP and FTP us...Control PanelChange LogBrowse PagesSearch?

HTTP and FTP using Ruby

Just to keep in touch with Ruby, I intend to write some utility programs in it every once in a while; stuff that'd make my life easier too.

Here's a utility program that fetches some log files (based on a date) using HTTP and uploads them to an FTP server, one at a time:

require 'net/http'
require 'net/ftp'
require 'uri'
require 'date'
 
FILE_PATH="./logs"
 
to_date=DateTime.now    
from_date=DateTime.parse(ARGV[0] || to_date.to_s)
 
url = URI.parse("http://www.not-a-real-site.com/logs")
 
host="ftp.another-fake-site.com"
username="fake_username"
password="my_password"
to_dir="/logs"
 
ftp=Net::FTP.open(host)
puts "CONNECTED TO FTP SITE"
ftp.login(username, password)
puts "CHDIR TO #{to_dir}"
ftp.chdir(to_dir)
 
from_date.upto(to_date) { |date|
    next if date.wday==6 or date.wday==0 # no logs on weekends
    puts "********************"
    puts "PROCESSING #{date}"
    req = Net::HTTP::Post.new(url.path)
    req.set_form_data({:date=>date.strftime("%Y%m%d")}, ';')
    req.add_field "Referer", "http://www.not-a-real-site.com/"
 
    req.basic_auth "admin", "password123"
 
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
    case res
    when Net::HTTPSuccess
      filename="Log-#{date.strftime("%Y%m%d")}.log"
      puts "SAVING TO #{filename}"
      file=File.new(FILE_PATH+"/"+filename, "w+b")
      file<<res.body
      file.flush
      file.close
      puts "SAVED #{filename}\r\n"
 
      begin
        ftp.connect(host)
        ftp.login(username, password)
        ftp.chdir(to_dir)
 
        puts "SENDING #{filename}"
        ftp.putbinaryfile(FILE_PATH+"/"+filename)
        puts "FTP STATUS: #{ftp.status}"
      rescue
        puts "FTP ERROR: #{$!}"
        ftp.connect(host)
        ftp.login(username, password)
        ftp.chdir(to_dir)	   
        retry
      end
 
    else
      puts "ERROR IN GETTING LOG FOR #{date}"
    end
}

Tags: technology:ruby, technology:coding Last modified 16:41 Fri, 2 Mar 2007 by AmanKing. Accessed 276 times Children What Links Here share Share Except where expressly noted, this work is licensed under a Creative Commons License.