9

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).

4

3 回答 3

12

我现在使用的解决方案使用“真正的”网络服务器,但配置起来更容易,即 lighttpd。我已将以下文件添加到我的路径中(在 中~/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 )))

这是一个 lighttpd 配置文件,充当 localhost 的反向代理;源和目标端口,以及服务器总最大带宽作为环境变量给出,因此可以像这样调用它:

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

在端口 8000 上代理 python 文件服务器,在端口 8001 上以每秒 60KB 的速度进行代理。显然,lighttpd 可用于提供文件本身,但这个小脚本可用于使任何http 服务器变慢

于 2012-12-01T05:48:21.783 回答
1

在 Windows 上,您可以使用 Fiddler,它是一种 HTTP 代理调试工具来模拟非常慢的速度。也许在您使用的任何操作系统上都存在类似的工具。

于 2012-12-01T00:03:03.640 回答
0

我记得我曾经有过同样的问题,我的搜索出现了一个名为mod_bw(即 mod_bandwith)的 Apache2 模块。它对我的测试很有帮助。

于 2012-11-30T23:58:31.497 回答