bottle---Python的輕量級http server

n24d 9年前發布 | 8K 次閱讀 Python

相比于Django而言,bottle顯得非常輕量級。短短幾行代碼即可快速搭建一個簡易的http server。提供了 Python Web開發中需要的基本支持:URL路由,Request/Response對象封裝,模板支持,與WSGI服務器集成支持。使用方法確實非常簡便。

import simplejson as son

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)</pre>

 本文由用戶 n24d 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!