• 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")#鍔犲瘑璁剧疆
    
    
    
    '''
    
    	
    

      

  • 相关阅读:
    Python定时任务sched(一)
    Python使用selenium进行爬虫(一)
    有关在python中使用Redis(二)
    有关JSOUP学习分享(一)
    jsoup爬虫,项目实战,欢迎收看
    有关在python中使用Redis(一)
    俄罗斯方块代码
    Android !No Launcher activity found!错误
    让jar程序在linux上一直执行(转)
    MyEclipse打包jar 并加入第三方包
  • 原文地址:https://www.cnblogs.com/dongfangchun/p/9241423.html
Copyright © 2020-2023  润新知