Terry : Ruby Tips

Ruby HTTP Server

Akin to SimpleHTTPServer (module) in Python 2, server.http in Python 3.

WEBRick

Complex version

Command

ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8080, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"

Ruby Script

require 'webrick'
root = File.expand_path '~/public_html'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
#To run the server you will need to provide a suitable shutdown hook as starting the server blocks the current thread
trap 'INT' do server.shutdown end
# trap 'INT' { server.shutdown }
server.start

Simple version

ruby -run -e httpd . -p 8080
# Use /path/to as DocumentRoot and bind to port PORT
ruby -run -e httpd /path/to -p PORT

NOTE:

-r requires the named library before executing, -run == require un.rb which is a set of utilities to replace common UNIX commands in Makefiles etc.

-e 'command'

Executes command as one line of Ruby source. Several -e's are allowed, and the commands are treated as multiple lines in the same program. If program file is omitted when -e is present, execution stops after the -e commands have been run.

Rack

Run Rack Server, current working directory as server document root

rackup -b "run Rack::Directory.new '.'"

Python

Run a simple HTTP Server, currently working directory as document root

Python 2.x

python -m SimpleHTTPServer 8080

Python 3.x

python -m http.server 8080