V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2012-11-12 22:50:33 +08:00 · 3037 次点击
    这是一个创建于 4172 天前的主题,其中的信息可能已经有所发展或是发生改变。
    做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 条回复    1970-01-01 08:00:00 +08:00
    BigZ
        1
    BigZ  
    OP
       2012-11-12 22:50:58 +08:00
    代码格式乱掉了,可以到这里 http://lutaf.com/53.htm 复制
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4932 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:01 · PVG 18:01 · LAX 03:01 · JFK 06:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.