我知道这个问题已经出现了很多,但我找不到任何合适的解决方案。从库requests
我尝试使用这样的东西:
response = requests.post(url, headers, data, proxies)
if response.status_code >= 300:
logging.ERROR(f"MY ERROR MESSAGE")
elif response.status_code < 300:
logging.INFO(f"MY INFO MESSAGE")
else:
logging.ERROR(f"MY ERROR MESSAGE 2")
这总是返回以下错误消息:
Traceback (most recent call last):
File "/var/task/my_little_file.py", line 36, in my_function
logging.INFO(f"MY INFO MESSAGE")
如果我打印出响应、响应的类型、状态码和状态码的类型,我将收到以下输出:
print("this is the response {}".format(response))
print(type(response))
print("status code: {}".format(response.status_code))
print("type status code: {}".format(type(response.status_code)))
>>> this is the response <Response [201]>
>>> <class 'requests.models.Response'>
>>> status code: 201
>>> type status code: <class 'int'>
所以status_code是一个整数,但不知何故if循环没有正确地解释它。任何帮助都是感激的:-)
我在Python 3.9上运行这个
问题不在于requests
,而在于如何使用logging
。logging
中所有大写的东西都是常量,如果你想使用函数,请使用全小写,即替换
logging.ERROR(f"MY ERROR MESSAGE")
使用
logging.error(f"MY ERROR MESSAGE")
和
logging.INFO(f"MY INFO MESSAGE")
使用
logging.info(f"MY INFO MESSAGE")
作为旁注,注意else-branch将永远不会到达,因为状态码要么大于等于300,要么小于300,tertium non datur。