I’ve had this post saved as a draft for two years, so I finally decided to clean it up a little and publish it in case anyone else finds the information useful.
In web development, for both client and server testing, it’s often very useful to have a local web server running in order to serve test cases. This post outlines a few quick and easy ways to run a local web server that don’t involve installing Apache or nginx and all their dependencies.
The simplest way I know of to get a local web server is with netcat:
brandon@dexter:~$ nc -l 8000
Then, when you hit the URL http://localhost:8000 in your browser, you will see the request show up on the terminal where netcat is running:
GET / HTTP/1.1
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:6.0a1) Gecko/20110513 Firefox/6.0a1
Accept-Encoding: gzip, deflate
You can then type an arbitrary response back to the browser, terminating the stream with Ctrl+C:
HTTP/1.1 200 OK
The hello world HTML page shows up in your browser as you would expect. You could just as easily use an existing file on your filesystem as input for the response:
brandon@dexter:~$ nc -l 8000 < http_response_file.txt
Another option to consider, especially if you already have test cases or other static content ready to serve, is Python’s SimpleHTTPServer class. You don’t even need to write a script to run one of these servers locally:
brandon@dexter:~$ python -m SimpleHTTPServer 8000
Serving HTTP on 0.0.0.0 port 8000 ...d
This time, hitting http://localhost:8000 in your browser will bring up a directory listing with your current working directory as the document root. Your requests are also logged on your Python terminal:
localhost - - [16/May/2011 14:55:36] "GET / HTTP/1.1" 200 -
On a separate but related note, you can get a local SMTP server running just as easily:
$ sudo python -m smtpd -c DebuggingServer -n localhost:25
---------- MESSAGE FOLLOWS ----------
Subject: Veracode report for 2013-10-24
Content-Type: text/plain; charset="us-ascii"
= Plain text version =
Content-Type: text/html; charset="us-ascii"
------------ END MESSAGE ------------