使用bottle快速开发server程序
做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)
浙公网安备 33010602011771号