【问题标题】:How can I modify python warnings?如何修改 python 警告?
【发布时间】:2019-10-03 06:12:10
【问题描述】:

我有一个需要修改 python 警告的要求,即如果我从代码中收到警告,我需要打印“用户友好”的警告消息。我不想要标准输出上的文件名。

with requests.get(url, stream=True, timeout=300,verify='/etc/ssl/certs/') as r:

如果 URL 是自签名的,我会收到一条打印文件名的警告。

【问题讨论】:

标签: python python-2.7 warnings


【解决方案1】:
 with warnings.catch_warnings(record=True) as w:
    with requests.get(url, stream=True, timeout=300, verify='/etc/ssl/certs/') as r:
       if w: 
          print w[0].message

如果 request.get() 返回任何警告,那么该警告将在 w[0].message 变量中可用,如果你想修改它,你可以。

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2019-08-08
    • 2020-11-28
    • 2017-10-25
    • 2017-07-03
    • 1970-01-01
    • 2021-12-07
    • 2017-10-04
    • 2011-11-27
    相关资源
    最近更新 更多