令人讨厌的CryptographyDeprecationWarning因为缺少hmac.compare_time函数无处不在

Dus*_*rea 6 python urllib urllib3 python-requests

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

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

我不知道它为什么开始它会破坏应用程序的/工具输出,特别是当它被其他工具捕获和使用时.像往常一样的许多困难,我相当肯定它与urllib关联有关requests.更糟糕的是,我有很多项目和交叉依赖项,我无法通过调用来更新所有导入和分支warnings.filterwarnings()以抑制警告.

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

我的Ubuntu,Python,urllib,请求(带有安全选项),加密和hmac都是打印警告的系统上的相同版本/版本,而不是.

在线似乎没有相关的警告或公告,此时似乎任何相关项目都是静态/稳定的(即使'hmac'可以通过PIP安装,它在八年内没有改变).

mbe*_*ima 23

我打了这个错误很长一段时间.对于我的环境,将Python升级到2.7.6以上的版本是一件痛苦的事.更简单的解决方案是使用pip降级加密模块:

pip2.7 install cryptography==2.2.2
Run Code Online (Sandbox Code Playgroud)

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


Jas*_*rew 10

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

\n
import warnings\nfrom cryptography.utils import CryptographyDeprecationWarning\nwarnings.filterwarnings("ignore", category=CryptographyDeprecationWarning)\n
Run Code Online (Sandbox Code Playgroud)\n

...嗯,我很确定上述内容在发布时确实有效,或者在某些情况下仍然有效但最近我发现一种情况,它没有抑制警告,而 Nikolaj \xc5\xa0 的评论中的这个建议。做过:

\n
import warnings\n# Suppress the deprecation warning from the cryptography module.\nwith warnings.catch_warnings():\n    warnings.simplefilter("ignore")\n    import cryptography\n
Run Code Online (Sandbox Code Playgroud)\n

所以... 随心所欲!

\n


Vic*_*era 7

此答案适用于 Python3

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

import warnings 
warnings.filterwarnings(action='ignore',module='.*paramiko.*')
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助

  • 最初的问题是一般的 Python 加密警告。这与帕拉米科无关。 (3认同)
  • @DustinOprea 明白,也许我应该澄清 Paramiko 正在为我抛出 CryptographyDeprecationWarning。因此我的回答是这样的。如果令人困惑,我们深表歉意。 (2认同)
  • 应该导入`warnings`而不是`warning` (2认同)