我正在尝试制作一个简单的工具来解密 GPG 消息,但是在解密消息时我不断收到错误消息,我不知道问题出在哪里。
这是代码:
import gnupg
import os
message = '''-----BEGIN PGP MESSAGE-----
Version: GnuPG v2
jA0ECQMCVady3RUyJw3X0kcBF+zdkfZOMhISoYBRwR3uk3vNv+TEg+rJnp4/yYIS
pEoI2S82cDiCNBIVAYWB8WKPtH2R2YSussKhpSJ4mFgqyOA01uwroA==
=KvJQ
-----END PGP MESSAGE-----
'''
passphrase = 'topsecret'
gpg = gnupg.GPG(os.popen("which gpg").read().strip())
decrypted_data = str(gnupg.decrypt(message, passphrase=passphrase))
print(decrypted_data)
这是错误:
PermissionError: [Errno 13] Permission denied: ''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 29, in <module>
gpg = gnupg.GPG(os.popen("which gpg").read().strip())
File "/Users/***/PycharmProjects/untitled8/venv/lib/python3.6/site-packages/gnupg.py", line 849, in __init__
raise OSError(msg)
OSError: Unable to run gpg () - it may not be available.
最佳答案
python-gnupg
软件包要求您安装 gpg
可执行。
来自 Deployment Requirements文档:
Apart from a recent-enough version of Python, in order to use this module you need to have access to a compatible version of the GnuPG executable. The system has been tested with GnuPG v1.4.9 on Windows and Ubuntu. On a Linux platform, this will typically be installed via your distribution’s package manager (e.g.
apt-get
on Debian/Ubuntu). Windows binaries are available here – use one of thegnupg-w32cli-1.4.x.exe
installers for the simplest deployment options.
你得到的错误很明显:
OSError: Unable to run gpg () - it may not be available.
它来自这一部分:
os.popen("which gpg").read().strip()
尝试运行
which gpg
从终端/控制台。如果您没有 gpg
已安装,which gpg
将评估为空字符串 ''
,然后整行将评估为空 ''
字符串。它实际上与gpg = gnupg.GPG('')
然后将引发相同的错误,即找不到
gpg
可执行。所以,基本上,你需要安装
gpg
第一的。如果您已经拥有
gpg
安装并出于某种原因 which gpg
就是找不到,你也可以传一个 gpgbinary
parameter :gpg = gnupg.GPG(gpgbinary="/usr/local/bin/gpg")
关于python - 使用 python-gnupg 获取 "OSError: Unable to run gpg () - it may not be available."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463965/