前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flask 学习-68. abort() 退出请求

Flask 学习-68. abort() 退出请求

作者头像
上海-悠悠
发布2022-09-19 15:43:36
5420
发布2022-09-19 15:43:36
举报
文章被收录于专栏:从零开始学自动化测试

前言

使用 abort() 可以 更早退出请求,并返回错误代码

abort() 函数

使用abort函数可以立即终止视图函数的执行,并可以返回特定的信息

代码语言:javascript
复制
abort(404)  # 404 Not Found
abort(Response('Hello World'))

源码介绍

代码语言:javascript
复制
def abort(
    status: t.Union[int, "Response"], *args: t.Any, **kwargs: t.Any
) -> "te.NoReturn":
    """Raises an :py:exc:`HTTPException` for the given status code or WSGI
    application.

    If a status code is given, it will be looked up in the list of
    exceptions and will raise that exception.  If passed a WSGI application,
    it will wrap it in a proxy WSGI exception and raise that::

       abort(404)  # 404 Not Found
       abort(Response('Hello World'))

    """
    _aborter(status, *args, **kwargs)

使用示例

代码语言:javascript
复制
from flask import Flask, request, g, abort, Response
app = Flask(__name__)

@app.route("/demo", methods=["GET"])
def demo():
    if not request.args.get('user'):
        abort(400)  # 400 bad request
    else:
        res = Response('hello world')
        return abort(res)

注意,状态码要出现在Flask定义的异常号列表(the list of exceptions)中,否则会引发内部服务器错误,比如,传递206,307就会报错

代码语言:javascript
复制
LookupError
LookupError: no exception for 307

exceptions 异常列表

异常列表定义在werkzeug.exceptions.default_exceptions中。 使用pprint可以查看全部状态码和对应的类

代码语言:javascript
复制
import pprint
import werkzeug
pprint.pprint(werkzeug.exceptions.default_exceptions)

运行结果

代码语言:javascript
复制
{400: <class 'werkzeug.exceptions.BadRequest'>,
 401: <class 'werkzeug.exceptions.Unauthorized'>,
 403: <class 'werkzeug.exceptions.Forbidden'>,
 404: <class 'werkzeug.exceptions.NotFound'>,
 405: <class 'werkzeug.exceptions.MethodNotAllowed'>,
 406: <class 'werkzeug.exceptions.NotAcceptable'>,
 408: <class 'werkzeug.exceptions.RequestTimeout'>,
 409: <class 'werkzeug.exceptions.Conflict'>,
 410: <class 'werkzeug.exceptions.Gone'>,
 411: <class 'werkzeug.exceptions.LengthRequired'>,
 412: <class 'werkzeug.exceptions.PreconditionFailed'>,
 413: <class 'werkzeug.exceptions.RequestEntityTooLarge'>,
 414: <class 'werkzeug.exceptions.RequestURITooLarge'>,
 415: <class 'werkzeug.exceptions.UnsupportedMediaType'>,
 416: <class 'werkzeug.exceptions.RequestedRangeNotSatisfiable'>,
 417: <class 'werkzeug.exceptions.ExpectationFailed'>,
 418: <class 'werkzeug.exceptions.ImATeapot'>,
 422: <class 'werkzeug.exceptions.UnprocessableEntity'>,
 423: <class 'werkzeug.exceptions.Locked'>,
 424: <class 'werkzeug.exceptions.FailedDependency'>,
 428: <class 'werkzeug.exceptions.PreconditionRequired'>,
 429: <class 'werkzeug.exceptions.TooManyRequests'>,
 431: <class 'werkzeug.exceptions.RequestHeaderFieldsTooLarge'>,
 451: <class 'werkzeug.exceptions.UnavailableForLegalReasons'>,
 500: <class 'werkzeug.exceptions.InternalServerError'>,
 501: <class 'werkzeug.exceptions.NotImplemented'>,
 502: <class 'werkzeug.exceptions.BadGateway'>,
 503: <class 'werkzeug.exceptions.ServiceUnavailable'>,
 504: <class 'werkzeug.exceptions.GatewayTimeout'>,
 505: <class 'werkzeug.exceptions.HTTPVersionNotSupported'>}

2022年第 12期《python接口web自动化+测试开发》课程,9月17号开学!

本期上课时间:2022年9月17号 - 2022年12月17号,周六周日上午9:00-11:00

报名费:报名费3000一人(周期3个月)

联系微信/QQ:283340479

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • abort() 函数
  • exceptions 异常列表
    • 报名费:报名费3000一人(周期3个月)
      • 联系微信/QQ:283340479
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档