首页 > 编程语言 > 详细

python微框架Bottle

时间:2015-07-06 21:44:21      阅读:293      评论:0      收藏:0      [点我收藏+]

目前项目中需要添加一个激活码功能,打算单独弄一个http服务器来写。

因为之前的游戏中已经有了一套生成激活码和激活码验证的http服务器,所以直接拿过来使用了。


Bottle是一个非常精致的WSGI框架,它提供了 Python Web开发中需要的基本支持:

URL路由,
Request/Response对象封装,
模板支持,
与WSGI服务器集成支持。


环境:
win7系统
Python2.7



一 下载

地址:http://bottlepy.org/docs/dev/index.html
只有一个bottle.py文件,没有任务标准库之外的依赖。


二 测试

新建文件useBottle.py,内容如下:
from bottle import route, run


@route('/hello') #将路由/hello关联到函数hello()
def hello():
    return "Hello World!"


run(host='localhost', port=8080, debug=True)

三 运行结果

技术分享

技术分享


四 稍微复杂一点的例子

from bottle import Bottle, route, run, template, error

app = Bottle()

@app.route('/hello')
def hello():
    return "Hello World!"


@app.route('/') # 缺省路由
@app.route('/hello/<name>') # hello下的所有路由
def greet(name='Stranger'):
    return template('Hello {{name}}, how are you?', name=name)


@app.error(404)
def error404(error):
    return 'Nothing here, sorry'


run(app, host='localhost', port=8080)


还可以用如下格式返回静态文件:
@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='/path/to/your/static/files')


参考:
http://bottlepy.org/docs/dev/tutorial.html

版权声明:本文为博主原创文章,未经博主允许不得转载。

python微框架Bottle

原文:http://blog.csdn.net/xufeng0991/article/details/46778935

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!