热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

请求-如何判断您是否收到成功消息?

如何解决《请求-如何判断您是否收到成功消息?》经验,为你挑选了1个好方法。

我的问题与这个问题密切相关.

我正在使用Requests库来命中HTTP端点.我想检查一下响应是否成功.

我目前正在这样做:

r = requests.get(url)
if 200 <= response.status_code <= 299:
    # Do something here!

而不是对200到299之间的值进行那种丑陋的检查,我可以用一个简写吗?



1> wim..:

该回复有一个ok属性.用那个.

@property
def ok(self):
    """Returns True if :attr:`status_code` is less than 400.

    This attribute checks if the status code of the response is between
    400 and 600 to see if there was a client error or a server error. If
    the status code, is between 200 and 400, this will return True. This
    is **not** a check to see if the response code is ``200 OK``.
    """
    try:
        self.raise_for_status()
    except HTTPError:
        return False
    return True


推荐阅读
author-avatar
caoxingchi_411
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有