A simple Ruby webserver for your testing infrastructure
On occasion, I run into the need to host some files on a web server to perform some testing. Whether it’s testing an embedded solution on smart, multi-function printers, serving files for mobile testing or facilitating some piece of automation, it’s nice to have a throw away web server when you need it.
Several scripting languages can pull this off, but since I’m partial to Ruby, I thought I’d offer up the Ruby flavored one-liner I use on occasion. I find this useful as it’s easy to just whip out on a command line any time I need it or fold it into more complex workflows.
Just type this in at the command line, assuming you have Ruby installed, and you’ll have a simple little file server up and running in seconds:
ruby -run -e httpd . -p 5000
Just a quick breakdown on what we’ve done here:
ruby -run -e
- first, we’re kicking off the ruby interpreter, telling it we’re going to run an evaluated expression
httpd .
- then the first part of our expression, indicating we’re wanting to run Ruby’s built in WEBrick web server, followed by a “.” (yes, a period). This simply says “hey, show everyone the files I have in my present working directory (pwd)”. Or you can supply a path instead, like “/path/to/my/adhoc/web/directory”, with no quotes. That’s what I usually do.
-p 5000
- is telling WEBrick to serve the files on port 5000, so you would use a URL such as “http://localhost:5000” to see the index of this server. Modify your port # if necessary.
To kill it, a simple CTRL + c
will end your process.
I have a log hoarder version I’ll share later.