How can I serve HTTP slowly?

http, load-testing

Solution

The solution I'm going with for now uses a "real" webserver, but a much easier to configure one, lighttpd. I've added the following file to my path (its in `~/bin`)

#! /usr/sbin/lighttpd -Df

server.document-root = "/dev/null"
server.modules = ("mod_proxy")
server.kbytes-per-second = env.LIGHTTPD_THROTTLE
server.port = env.LIGHTTPD_PORT
proxy.server  = ( "" => (( "host" => "127.0.0.1", "port" => env.LIGHTTPD_PROXY )))

Which is a lighttpd config file that acts as a reverse proxy to localhost; source and destination ports, as well as a server total maximum bandwidth are given as environment variables, and so it can be invoked like:

$ cd /path/to/some/files
$ python -m SimpleHTTPServer 8000 &
$ LIGHTTPD_THROTTLE=60 LIGHTTPD_PORT=8001 LIGHTTPD_PROXY=8000 throttle.lighttpd

to proxy the python file server on port 8000 with a low 60KB per second on port 8001. Obviously, lighttpd could be used to serve the files itself, but this little script can be used to make any http server slow

Problem

I'm working on an http client and I would like to test it on requests that take some time to finish. I could certainly come up with a python script to suit my needs, something about like: ``` def slow_server(environ, start_response): with getSomeFile(environ) as file_to_serve: block = file_to_serve.read(1024); while block: yield block time.sleep(1.0) block = file_to_serve.read(1024); ``` but this feels like a problem others have already encountered. Is there an easy way to serve static files with an absurdly low bandwidth cap, short of a full scale server like apache or nginx. I'm working on linux, and the way I've been testing so far is with `python -m SimpleHTTPServer 8000` in a directory full of files to serve. I'm equally interested in another simple command line server or a way to do bandwidth limiting with one or a few iptables commands on tcp port 8000 (or whatever would work).

Original source