我在我的python代码中使用了Paramiko (用于sftp)。除了每次我导入或调用paramiko函数之外,一切都很正常。将显示以下警告:
C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation
Warning: This application uses RandomPool, which is BROKEN in older releases. S
ee http://www.pycrypto.org/randpool-broken
RandomPool_DeprecationWarning)
我知道这与帕拉米科正在使用PyCrypto的一些过时功能有关。
我的问题是,有没有办法通过编程来抑制这个警告?我已经尝试过了:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')
甚至是这样:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')
在'import paramiko‘语句之前和paramiko特定的函数调用之前,但都不起作用。无论发生什么情况,这个警告都会不断出现。如果有帮助,下面是打印警告的第三方库中的代码:
在randpool.py中:
from Crypto.pct_warnings import RandomPool_DeprecationWarning
import Crypto.Random
import warnings
class RandomPool:
"""Deprecated. Use Random.new() instead.
See http://www.pycrypto.org/randpool-broken
"""
def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
warnings.warn("This application uses RandomPool, which is BROKEN in older releases. See http://www.pycrypto.org/randpool-broken",
RandomPool_DeprecationWarning)
如果您知道解决此问题的方法,请帮助我关闭此警告。
发布于 2010-10-13 12:59:48
最简单的方法是按照警告模块的建议使用here
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import paramiko
发布于 2018-12-10 21:40:36
warnings.filterwarnings
的module
参数采用区分大小写的正则表达式,该正则表达式应与完全限定的模块名称匹配,因此
warnings.filterwarnings(
action='ignore',
category=DeprecationWarning,
module=r'.*randpool'
)
或
warnings.filterwarnings(
action='ignore',
category=DeprecationWarning,
module=r'Crypto\.Utils\.randpool'
)
应该行得通。如果由于某种原因,RandomPool_DeprecationWarning
不是DeprecationWarning
的子类,那么您可能需要显式地编写RandomPool_DeprecationWarning
而不是DeprecationWarning
。
还可以在调用脚本时禁用命令行上的警告,方法是将-W
选项传递给解释器,如下所示:
$ python -W ignore::RandomPool_DeprecationWarning:Crypto.Utils.randpool: my_script.py
-W
采用action:message:category:module:lineno
格式的过滤器,此时module
必须与发出警告的(完全限定的)模块名称完全匹配。
请参阅https://docs.python.org/2/library/warnings.html?highlight=warnings#the-warnings-filter和https://docs.python.org/2/using/cmdline.html#cmdoption-w
发布于 2017-09-16 02:20:18
要仅筛选特定警告,请执行以下操作:
with warnings.catch_warnings():
warnings.simplefilter('ignore', SpecificWarningObject)
#do something that raises a Warning
https://stackoverflow.com/questions/3920502
复制相似问题