A simple web server
Sometimes it is useful to dish up files over a web connection. Say, when on a server, and you want to tunnel some code coverage back over ssh.
One way of doing this would be to use a simple python server:
python -m SimpleHTTPServer 8000
Python’s HTTP Server
However, the use of another programming language is surely unthinkable* to us Rubyists!
So how can I do it in Ruby?
ruby -run -e httpd . -p 8000
Ruby’s HTTP Server
What’s going on here? Let’s break it down, as it is confusing at first. It could also be written as:
ruby -r “un” -e “httpd” . -p 8000
-r requires a library, in this case “un.rb”. This is a library containing a few useful methods, one of which is httpd: https://github.com/sj26/ruby-1.9.3-p0/blob/master/lib/un.rb,
-e executes a ruby command
. passes the current directory as an argument, thanks to shell expansion, bash expands this to be the current path.
-p is an option passed to the running ruby process once its started.
While there is no real difference to avoiding python in this case, it is nice to work with a language that you have a higher degree of familiarity with!
These simple web servers can be useful for dishing up files over an SSH connection for example:
ssh -L 8000:localhost:8000 my.web.server.com $ python -m SimpleHTTPServer 8000
Then navigate to
localhost:8000 in your web browser to view images and other files!
- Not unthinkable, it is wise for a developer to survey the tools available and make an educated decision!