推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
BigZ
V2EX  ›  Python

使用bottle快速开发server程序

  •  
  •   BigZ · Nov 12, 2012 · 3862 views
    This topic created in 4933 days ago, the information mentioned may be changed or developed.
    做web产品,经常会有某个功能需要加载大量数据,比如中文分词,汉字转拼音等 或者你需要异构化设计某个产品,http render 用php,计算或者采集程序用python 这种情况下,最好的方式是把这个功能抽离出来,做成http服务

    python的web 框架有很多,简单轻量级的single file 框架有

    webpy
    flask
    bottle
    webpy现在越做越大,flask和bottle其实差不多,关于web开发框架,可以看这篇文章 如何选择web开发框架,我一直用bottle,总结出如下经验

    bottle需要升级到0.11 版本以上才正确,之前版本有很多莫名bug easy_install -U bottle即可
    处理post请求,参数从 request.forms.varname 提取
    处理get请求,参数从 request.query.varname 提取,同时要import get,post,request,等名字
    如果想debug一下,加一行代码 debug(1)
    默认的http server是使用python内置的SimpleHTTPServer,一秒钟可以处理10几个请求,做内部dameon的server是足够了 如果需要高性能,或者要对brower用户开放,Tornado 是最佳选择

    一个分析网页的server

    #coding:utf-8


    from bottle import route, run,request,post,debug,get
    import lutaf


    @post('/tag')
    def index():
    content = request.forms.data
    tags = lutaf.analyse(content,topK=5)
    return ','.join(tags)

    @route('/status')
    def index():
    return 'auto tag server ...'

    run(port=8090)
    1 replies    1970-01-01 08:00:00 +08:00
    BigZ
        1
    BigZ  
    OP
       Nov 12, 2012
    代码格式乱掉了,可以到这里 http://lutaf.com/53.htm 复制
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3119 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 13:02 · PVG 21:02 · LAX 06:02 · JFK 09:02
    ♥ Do have faith in what you're doing.