Python 警告过滤器未捕获 InsecurePlatformWarning

Python warnings filter not catching InsecurePlatformWarning

自从这条消息

lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning

一直在充斥我的日志(是的,我知道这很重要,我最终会解决真正的问题!我保证!),我想添加一个过滤器让它只显示一次。 所以我添加了这个:

warnings.simplefilter(action='once', category=InsecurePlatformWarning, append=True)

但它不过滤任何东西。 当我这样做时:

warnings.simplefilter(action='once', append=True)

它对所有警告都这样做,这不是我想要的,但它表明代码确实正在执行并且过滤器本身确实有效。

我在类别方面做错了什么? 我不想禁用警告。我只想让这个特定警告暂时只显示一次。

谢谢!

尝试删除 append=True:

warnings.simplefilter(action='once', category=InsecurePlatformWarning)

urllib3 代码already has default logging configuration。如果您使用 append=True,则默认配置会覆盖您的配置。