前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >bottle---Python的轻量级http server

bottle---Python的轻量级http server

原创
作者头像
艳艳代码杂货店
修改2021-11-02 11:18:25
5250
修改2021-11-02 11:18:25
举报

相比于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> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档