Python --- Bottle: 轻量级Web Server

相比于Django而言,bottle显得非常轻量级。短短几行代码即可快速搭建一个简易的http server。提供了 Python Web开发中需要的基本支持:URL路由,Request/Response对象封装,模板支持,与WSGI服务器集成支持。使用方法确实非常简便。


import simplejson as json

from bottle import Bottle, route, run, request, response, get, post

app = Buttle()


@app.route("/index.html")

def index():

    return '<a href="/hello">Hello world</a>'


# 下边的例子根据/hello/xxx的url进行动态路由。

@app.route("/hello/:name")

def helloName(name):

    page = request.GET.get('page', '1')

    return '<h1>Hello %s <br/>(%s)</h1>' % (name, page)


@app.get('/getRequest')

def getRequest():

    return json.dumps({"status": 0, "data": "data"})


@app.post("/postRequest")

def postRequest():

    data = request.forms

    cmd = data.get('cmd', None)

    return json.dumps({"status": 0, "data": "data"})


if __name__ == "__main__":

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值