eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

Piping logs and stuff through HTTP

Ever wanted to pipe a tail -f through HTTP? I made a quick hack in order to use the excellent rikaichan extension for Firefox in real-time IRC conversations:

rikaichan.png

I was going to code a File::Tail but a quick look at the RAA pointed me to Florian Frank's file-tail, making the remaining code trivial:

#!/usr/bin/env ruby
require 'tail'
require 'socket'
require 'cgi'

port = 54321

sock  = TCPServer.new('localhost', port)
loop do
  Thread.start(sock.accept) do |conn|
    conn.sync = true
    conn.puts <<EOF
HTTP/1.0 200 OK
Content-Type: text/html


<html><body>
EOF
    File::Tail::Logfile.open(ARGV[0], :rewind => 10) do |log|
      log.tail { |line| conn.puts "#{CGI::escapeHTML(line)}<br />" }
    end
  end
end


Last modified:2006/03/31 06:26:18
Keyword(s):[blog] [ruby] [tail] [web] [firefox] [rikaichan] [japanese] [snippet] [subpar] [frontpage]
References:[Ruby]