小编Tom*_*yen的帖子

如何使用warnings.filterwarnings抑制第三方警告

我在我的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)
Run Code Online (Sandbox Code Playgroud)

我知道这与Paramiko正在使用PyCrypto的一些不推荐的功能这一事实有关.

我的问题是,有没有办法以编程方式抑制此警告?我试过这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')
Run Code Online (Sandbox Code Playgroud)

甚至这个:

warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')
Run Code Online (Sandbox Code Playgroud)

在'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)
Run Code Online (Sandbox Code Playgroud)

如果您知道解决方法,请帮我关闭此警告.

python paramiko suppress-warnings pycrypto

33
推荐指数
4
解决办法
2万
查看次数

标签 统计

paramiko ×1

pycrypto ×1

python ×1

suppress-warnings ×1