我在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)
我知道这与Paramiko正在使用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)
如果你知道解决这个问题的方法,请帮我关闭这个警告。
最简单的方法是,正如警告模块建议的那样here:
仅筛选特定警告:
warnings.filterwarnings
的module
参数采用区分大小写的正则表达式,该表达式应与完全限定的模块名匹配,因此或者
应该有用。如果由于某种原因
RandomPool_DeprecationWarning
不是DeprecationWarning
的子类,则可能需要显式地编写RandomPool_DeprecationWarning
,而不是DeprecationWarning
。当调用脚本时,还可以通过将
-W
选项传递给解释器来禁用命令行上的警告,如下所示:-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
相关问题 更多 >
编程相关推荐