028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Bottle框架源码学习三-创新互联

def run(app=None, server='wsgiref', host='127.0.0.1', port=8080,
        interval=1, reloader=False, quiet=False, plugins=None,
        debug=None, **kargs):

今天要学习一下bottle里是怎样打印debug信息的

成都创新互联公司2013年开创至今,是专业互联网技术服务公司,拥有项目网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元白银做网站,已为上家服务,为白银各地企业和个人服务,联系电话:18980820575

run函数的倒数第二个参数是debug,默认为None

try:
    if debug is not None: _debug(debug)

如果设置了debug的值,且不为None,则运行_debug函数

_debug = debug

_debug函数是debug函数的别名,因为和run里的debug变量同名,为了区别,所以用_debug这个名称

def debug(mode=True):
    """ Change the debug level.
    There is only one debug level supported at the moment."""
    global DEBUG
    if mode: warnings.simplefilter('default')
    DEBUG = bool(mode)

globa DEBUG这句的作用是声明DEBUG这个变量是全局变量,由于要修改它的值,如果不声明为全局变量,则会将DEBUG定义为本函数内的局部变量。

if mode: warnings.simplefilter('default')

warnings.simplefilter定义简单过滤器,如果mode为真,则warnings的过滤器为default,以下是几种过滤器参数,特别要说明的是error过滤器,如果应用了这个过滤器,一旦产生警告信息,则当成错误来处理,不再执行后面的语句。

好了,大概用法已经看懂,再看bottle是怎样应用的

# -*- coding=utf-8 -*-
from bottle import Bottle, run

app = Bottle()

@app.route('/hello')
def hello():
    raise Exception("this is my error")
    return "Hello World!"

run(app, host='localhost', port=8080, reloader=True, debug=True)

将debug设置为True, 并在hello函数里手工制造一个异常。

浏览器里访问http://localhost:8080/hello

结果如下:

Error: 500 Internal Server Error

Sorry, the requested URL 'http://localhost:8080/hello' caused an error:

Internal Server Error

Exception:

Exception('this is my error',)

Traceback:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\bottle.py", line 862, in _handle
    return route.call(**args)
  File "C:\Python27\lib\site-packages\bottle.py", line 1740, in wrapper
    rv = callback(*a, **ka)
  File "D:/myproject/bottleApp/main.py", line 8, in hello
    raise Exception("this is my error")
Exception: this is my error

看模板源码是怎样设置的


    

Error: `e`.`status`

    

Sorry, the requested URL {{repr(request.url)}}        caused an error:

    
`e`.`body`
    %%if DEBUG and e.exception:       

Exception:

      
{{repr(e.exception)}}
    %%end     %%if DEBUG and e.traceback:       

Traceback:

      
`e`.`traceback`
    %%end

注:如果debug=False,则不会输出Traceback信息,生产环境一般是要关闭的,开发的时候打开方便排错。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:Bottle框架源码学习三-创新互联
文章来源:http://www.tsicrk.com/article/cdeshh.html

其他资讯

让你的专属顾问为你服务

2.3154s