爱意满满的作品展示区。
wangleineo

用 Python 写了个 web server,求测试

  •  1
     
  •   wangleineo ·
    RealHacker · Sep 28, 2015 · 3284 views
    This topic created in 3904 days ago, the information mentioned may be changed or developed.

    写了一个极简单的 web server ,支持:

    • 静态文件(相当于标准库 SimpleHTTPServer )
    • WSGI 应用 (简单的 WSGI Server )
    • 代理到其他 web 服务
    • 多线程 /多进程处理请求

    代码

    https://github.com/RealHacker/python-gems/tree/master/http_server

    配置文件

    {
        "server":{
            "ip": "127.0.0.1",
            "port": 8000,
            "mode": "thread"
        },
        "routes":{
            "/app": {
                "type": "wsgi",
                "application": "D:/Workspace/Example/wsgi.py"
            },
            "/app/static": {
                "type": "static",
                "dir":  "D:/workspace/static/"
            }, 
            "/app/proxy": {
                "type": "proxy",
                "proxyurl": "http://www.qq.com/"
            }
        }
    }
    

    设置好 config.json 后,即可执行:

    python web_server.py
    

    这个是自己写的原型,所以一定有很多没考虑到的问题,希望有兴趣的可以帮忙跑一下,看看有什么 bug ,或者直接点评代码。良辰在此先行谢过。

    7 replies    2015-10-09 20:39:41 +08:00
    noli
        1
    noli  
       Sep 29, 2015
    这种测试服务器的事情,自己写个脚本压测一下就什么问题都可以发现了啦……
    hiroya
        2
    hiroya  
       Sep 29, 2015 via iPad
    …… Windows 目录……看了下代码, Linux 应该兼容吧………_(:3 」∠)_
    wangleineo
        3
    wangleineo  
    OP
       Sep 29, 2015
    @noli 不是测性能 是测功能
    @hiroya Linux 可以
    sengxian
        4
    sengxian  
       Oct 2, 2015   ❤️ 1
    @wangleineo 安新系统时刚好用了下你的服务器,还不错 http://seng.ink/app-store-os-x-el-captian
    wangleineo
        5
    wangleineo  
    OP
       Oct 2, 2015
    @sengxian 多谢支持!
    saber000
        6
    saber000  
       Oct 9, 2015
    proxy 模式下对头部处理会有点问题:
    1. 把请求的 cookie 删掉是否不合适?
    2. 返回不光要去掉 content-encoding 和 transfer-encoding,可以参考:
    https://github.com/MrLYC/post_proxy/blob/master/proxy/server.py#L85
    wangleineo
        7
    wangleineo  
    OP
       Oct 9, 2015
    @saber000 受教!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2828 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 77ms · UTC 04:27 · PVG 12:27 · LAX 21:27 · JFK 00:27
    ♥ Do have faith in what you're doing.