[英]gnupg - decrypt into Python bytesio stream
How can I select a stream as the output of a decrypt_file
operation in gnupg
? 如何在
gnupg
选择流作为decrypt_file
操作的输出?
The docs and the code seem to suggest this is not possible. 文档和代码似乎暗示这是不可能的。 If I am correct (see below), what workarounds are possible?
如果我是正确的(请参阅下文),可能有什么解决方法?
~~~ ~~~
The documentation seems to suggest it is not possible: 该文档似乎暗示不可能:
decrypt_file(filename, always_trust=False, passphrase=None, output=None)¶
with " output (str) – A filename to write the decrypted output to." 带有“ output(str) –写入解密输出的文件名。”
~~~ ~~~
Opening up the code , I see: 打开代码 ,我看到:
def decrypt_file(self, file, always_trust=False, passphrase=None,
output=None, extra_args=None):
args = ["--decrypt"]
if output: # write the output to a file with the specified name
self.set_output_without_confirmation(args, output)
if always_trust: # pragma: no cover
args.append("--always-trust")
if extra_args:
args.extend(extra_args)
result = self.result_map['crypt'](self)
self._handle_io(args, file, result, passphrase, binary=True)
logger.debug('decrypt result: %r', result.data)
return result
which points to set_output_without_confirmation
, confirming the idea is that you pass a string filename: 指向
set_output_without_confirmation
,确认想法是您传递了字符串文件名:
def set_output_without_confirmation(self, args, output):
"If writing to a file which exists, avoid a confirmation message."
if os.path.exists(output):
# We need to avoid an overwrite confirmation message
args.extend(['--yes'])
args.extend(['--output', no_quote(output)])
To output the decrypted data to a variable use decrypt
instead of decrypt_file
, as shown here in the " Decrypt a string " paragraph. 到输出解密后的数据给一个变量使用
decrypt
代替decrypt_file
,如图这里在“ 解密的字符串 ”段落。
So the original code: 所以原来的代码:
status = gpg.decrypt_file(input_file, passphrase='my_passphrase', output='my_output_file')
is substituted by: 替换为:
decrypted_data = gpg.decrypt(input_file.read(), passphrase='my_passphrase')
# decrypted_data.data contains the data
decrypted_stream = io.BytesIO(decrypted_data.data)
# this is py3, in py2 BytesIO is imported from BytesIO
As an example for the specific use case for csv data , building on this SO post , you could then do: 在此SO post的基础上 , 以csv数据的特定用例为例,您可以执行以下操作:
my_df = pandas.read_csv(decrypted_stream)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.