天天看點

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" target="_blank" rel="external nofollow" >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)