One-line shell script server for testing your AJAX and HTTP code

Wednesday, April 1st, 2009 by Jason

Here is how to make a very simple web server with a very simple shell script on a GNU or Unix system such as OS X. I have found it to be ideal for quick tests and debugging sessions with AJAX code and tools like Python’s urllib2.

The idea is to run an infinite loop which calls socat to return static files to the browser.

Preparation

  1. Install Bash and socat on your OS. Your operating system probably has packages named bash and socat.
  2. Create a new directory, for exmple responses/ to hold your static response files.
  3. Place one or more files in this directory. The format should be:
    1. HTTP response code
    2. HTTP Headers
    3. Blank line
  1. Content such as HTML, Javascript, or whatever you need

    Example response file.

    Here is what I put in my responses/simple-html.txt file. I got it from a from a tcpdump capture, but you could use Firebug or HTTPFox.

    
    HTTP/1.1 200 OK
    Connection: close
    Date: Tue, 31 Mar 2009 15:13:29 GMT
    Server: Microsoft-IIS/6.0
    X-Powered-By: ASP.NET
    X-AspNet-Version: 2.0.50727
    Set-Cookie: ASP.NET_SessionId=Z3X0u4H5iDieUuZL3KbAuaS7; path=/; HttpOnly
    Cache-Control: no-cache
    Pragma: no-cache
    Expires: -1
    Content-Type: text/html; charset=utf-8
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <title>Example Page</title>
        </head>
        <body>
            <h1>Isn't this cool?</h1>
        </body>
    </html>
    

    Running the server

    Running the server is pretty straightforward. It’s mostly a one-liner (split here for space), but you could format it and put it in a script. The idea is for the server to randomly choose a file from the directory. If there is only one, then you get a consistent response (and you can edit that file between trials of course). If there are more, then you can see how your app responds to different data.

    while true; do socat TCP4-LISTEN:8080,reuseaddr \
      SYSTEM:'cat `/bin/ls -1 responses/* | sort --random-sort | head -1`'; done

    Now, going to http://localhost:8080/ will show that page!

    Storyboarded response sequences

    You can extend this script to return a series of HTTP responses (with HTML, JSON, whatever) in them, like a storyboard. First, give the files names such that they are in the correct order when alphabetized (e.g. 01-first, 02-second, etc.). Then use a script like this:

    while true; do for f in responses/*; do socat TCP4-LISTEN:8080,reuseaddr \
      SYSTEM:"cat $f"; done; done

    Here, every time you query the server, it will return the next response in the queue. When the queue is empty, it will start over; and you can make changes to the storyboard between restarts.

    Concluding Notes

    • You can control the HTTP and HTML! So you can test 404s and 301s and anything else!
  • This works for any URL you try since it completely ignores the client request and headers
  • To make the “server” easier to kill, before the first “done” keyword, add: [ $? -ne 0 ] && break 2;
  • You could substitute Netcat for socat if you don’t have access to the latter
    • I haven’t tested this on OS, but I would love to hear from people about successes or bugs with the script.

    Posted in Free Software No Comments »

    Leave a Comment

    Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

    XHTML: You may use these tags: