bottle框架代码的组织

Posted dongfangchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bottle框架代码的组织相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import bottle
#bottle.debug(True)
import time,sys,os
import mysqldb

from bottle import route,error,template,request,response
from bottle import get,post
from bottle import static_file

from bottle import view


bottle.TEMPLATES.clear()

#template using view
@route("/template_file")
@route("/template_file/<name>")
@view("hello_template")
def hello(name="world"):
    return dict(name=name)

#environ
@route("/ipaddr")
def ipaddr():
    ip = request.environ.get("REMOTE_ADDR")
    #return ip
    return str(request.environ)
    

#template
@route("/hello/<name>")
def test(name="Stangeer"):
    return template("hello {{name}},8888",name=name)
	
#reponse application/json,濡傛灉杩斿洖瀛楀吀锛宑ontent type浼氳嚜鍔ㄨ浆涓? applicaton/json
@route("/json")
def json():
    return {"name":"scj","age":34,"ch_name":"鏉板厠"}

#dynamic routes
@route(‘/greet/<name>‘)
def greet(name):
    return "hello,"+name

#get method
@get(‘/gettest‘)
def gettest():
    return request.query.get(‘name‘)+request.query.get(‘age‘)
	

#post method
@post(‘/posttest‘)
def post_test():
    return request.forms.get(‘name‘)+request.forms.get(‘age‘)

#get post merged method by using route
@route(‘/routemethod‘)
def routemethod():
    v = request.query.get(‘name‘)
    v2 = request.forms.get(‘name‘)
    if v is None:
        return "get:none"
    if v2 is None:
        return "post:none"
    return "get:"+request.query.get(‘name‘)+","+request.forms.get(‘name‘)

#static file
@route(‘/statictest/<filepath:path>‘)
def static_test(filepath):
    return static_file(filepath,root=‘/var/www/html‘)
    
#not found page
@error(404)
def error404(error):
    return "12璁块棶鐨勯〉闈笉瀛樺湪404"

‘‘‘甯哥敤鍑芥暟
abort(401,"sorry,access denied")#鐩存帴鎶ラ敊

redirect("/right/url")#閲嶅畾鍚?

response.set_header("Content-Language","en")
response.set_header(‘Set-Cookie‘, ‘name=value‘)
response.add_header(‘Set-Cookie‘, ‘name2=value2‘)

#get method
id = request.query.id
id = request.query.get("id")

#cookie
str_temp = request.get_cookie("visited")#if no cookie,return None
str_temp = request.cookies.get("visited","defaultValue")
response.set_cookie("token", "12344321",max_age=100,path="/")
str_temp = request.get_cookie("visited",secret="mykey")#鍔犲瘑鑾峰彇
response.set_cookie("token", "12344321",max_age=100,path="/",secret="mykey")#鍔犲瘑璁剧疆



‘‘‘

	

  

以上是关于bottle框架代码的组织的主要内容,如果未能解决你的问题,请参考以下文章

python web 开发框架之Bottle

day-3.初识pythong框架bottle

Bottle 框架和 OOP,使用方法而不是函数

bottle框架学习:命令行

如何使用python bottle框架查询mongo数据库

Python轻量级Web框架:Bottle库!