码迷,mamicode.com
首页 > 其他好文 > 详细

bottle框架代码的组织

时间:2018-06-29 01:13:19      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:template   csharp   test   str   port   gre   pad   access   routes   

#!/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框架代码的组织

标签:template   csharp   test   str   port   gre   pad   access   routes   

原文地址:https://www.cnblogs.com/dongfangchun/p/9241423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!