Temporarily serve any directory via HTTP, with nice handling of privileged ports.

Put the following on your path in a file srv. Make it executable.

Then, just go to the directory you want and run srv

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/ruby

port = ARGV[0] || '80'

if port != 'sudo' and port.to_i < 1000
  puts "trying privileged port #{port} with sudo"
  exec "sudo #{$0} sudo #{port}"
end

if port == 'sudo'
  port = ARGV[1]
end

require 'webrick'
include WEBrick

s = HTTPServer.new(
  :Port            => port,
  :DocumentRoot    => Dir::pwd
)

s.mount("/", HTTPServlet::FileHandler, Dir::pwd, true)

trap("INT"){ s.shutdown }
s.start

Post a comment


( lesstile enabled - surround code blocks with ---)