gpt4 book ai didi

python - 请求——总是调用 raise_for_status

转载 作者:太空狗 更新时间:2023-10-29 20:48:59 25 4
gpt4 key购买 nike

我想删除重复的 x.raise_for_status() 行:

x = requests.get(url1)
x.raise_for_status()

y = requests.delete(url2)
y.raise_for_status()

z = requests.post(url3, data={'foo': 'bar'})
z.raise_for_status()

如何自动调用 raise_for_status()

最佳答案

使用钩子(Hook)创建 session :

session = requests.Session()
session.hooks = {
'response': lambda r, *args, **kwargs: r.raise_for_status()
}

x = session.get(url1)
y = session.delete(url2)
z = session.post(url3, data={'foo': 'bar'})

关于python - 请求——总是调用 raise_for_status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45470226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com