令人讨厌的 CryptographyDeprecationWarning 因为到处都缺少 hmac.compare_time 函数

Obnoxious CryptographyDeprecationWarning because of missing hmac.compare_time function everywhere

事情 运行 一切顺利,直到我的一个项目开始在每个地方打印这个,在每次执行的顶部,至少一次:

local/lib/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a 2.7.x release that supports hmac.compare_digest as soon as possible.

我不知道它为什么会启动,它会破坏应用程序/工具的输出,尤其是当它被其他工具捕获和使用时。就像整个时间的许多困难一样,我相当确定它与 urllib 相关,并且通过关联,requests。更糟糕的是,我有太多的项目和交叉依赖项,以至于我无法通过调用 warnings.filterwarnings() 来更新所有导入和分支以抑制警告。

我有 Python 2.7.6。显然这在 2.7.7 中消失了。只是,我有一些系统有 2.7.6,我 没有 看到警告。所以,某些东西可能会或可能不会在一个版本中禁用它们,我可能无意中将其替换为另一个版本。

我的 Ubuntu、Python、urllib、请求(带有安全选项)、加密和 hmac 在打印警告的系统和打印警告的系统上都是相同的 versions/builds不要。

网上似乎没有相关的警告或公告,似乎任何相关的项目都是static/stable(尽管'hmac'可以通过PIP安装,但它没有改变八年后)。

我开始收到此警告是为了直接调用 requests.get。此警告在加载模块 cryptography.hazmat.primitives.constant_time 时打印,因此通常每个 Python 程序只出现一次。如果您多次看到它,那一定是因为 Python 程序(如实用程序)被多次执行。您只需识别该程序并将以下代码添加到主入口点:

import cryptography
from cryptography import utils
with warnings.catch_warnings():
    warnings.simplefilter('ignore', cryptography.utils.DeprecatedIn23)
    import cryptography.hazmat.primitives.constant_time

我遇到这个错误已经有一段时间了。对于我的环境,将 Python 升级到高于 2.7.6 的版本是一件痛苦的事情。更简单的解决方案是使用 pip 降级密码模块:

pip2.7 install cryptography==2.2.2

我认为最好的解决方案是升级您的 python 版本

此答案适用于Python3

我在使用 Paramiko 时通过寻找答案来到这里。对于那些仍在寻找简单答案的人。在导入 Paramiko 之前,我用这些代码行抑制了这些 CryptographyDeprecationWarning:

import warnings 
warnings.filterwarnings(action='ignore',module='.*paramiko.*')

希望对您有所帮助

当你执行pip2.7 install cryptography==2.2.2时,貌似还是会报错。 我相信你也需要 sudo pip2.7 install --upgrade pip Aso,截至 2019 年 5 月 5 日,最新的似乎是 cryptography=2.6.1

仅限 Python3:

显然 Paramiko update 这对我有用并解决了我遇到的类似 problem/symptoms:

pip3 install --upgrade paramiko

这在我的系统上安装了 paramiko 2.6.0,替换了 2.4.2:

$ pip3 install --upgrade paramiko
[...]
Installing collected packages: paramiko
  Found existing installation: paramiko 2.4.2
    Uninstalling paramiko-2.4.2:
      Successfully uninstalled paramiko-2.4.2
Successfully installed paramiko-2.6.0
$

My Python2 environment appears to be messed up,所以我无法在 Python2.

上进行测试

如果您想更有选择性地只抑制特定的弃用警告:

import warnings
from cryptography.utils import CryptographyDeprecationWarning
warnings.filterwarnings("ignore", category=CryptographyDeprecationWarning)

我通过从 python 源代码 [your_installation_path]\Python\Lib\site-packages\cryptography\__init__.py

中删除警告,使用 Python 2 为我所有的本地项目和工具修复了这个问题

只需删除文件末尾的这个片段并删除 __init__.pyc 文件,以便根据更改重新编译:

if sys.version_info[0] == 2:
warnings.warn(
    "Python 2 is no longer supported by the Python core team. Support for "
    "it is now deprecated in cryptography, and will be removed in the "
    "next release.",
    CryptographyDeprecationWarning,
    stacklevel=2,
)