<small id='iyPJ6'></small><noframes id='iyPJ6'>

  • <i id='iyPJ6'><tr id='iyPJ6'><dt id='iyPJ6'><q id='iyPJ6'><span id='iyPJ6'><b id='iyPJ6'><form id='iyPJ6'><ins id='iyPJ6'></ins><ul id='iyPJ6'></ul><sub id='iyPJ6'></sub></form><legend id='iyPJ6'></legend><bdo id='iyPJ6'><pre id='iyPJ6'><center id='iyPJ6'></center></pre></bdo></b><th id='iyPJ6'></th></span></q></dt></tr></i><div id='iyPJ6'><tfoot id='iyPJ6'></tfoot><dl id='iyPJ6'><fieldset id='iyPJ6'></fieldset></dl></div>

      <tfoot id='iyPJ6'></tfoot>
      • <bdo id='iyPJ6'></bdo><ul id='iyPJ6'></ul>
        <legend id='iyPJ6'><style id='iyPJ6'><dir id='iyPJ6'><q id='iyPJ6'></q></dir></style></legend>
      1. gpg --passphrase-fd 不适用于 python 3 子进程

        时间:2023-07-21

          <tbody id='C16v8'></tbody>
        <tfoot id='C16v8'></tfoot>
          <bdo id='C16v8'></bdo><ul id='C16v8'></ul>
            <i id='C16v8'><tr id='C16v8'><dt id='C16v8'><q id='C16v8'><span id='C16v8'><b id='C16v8'><form id='C16v8'><ins id='C16v8'></ins><ul id='C16v8'></ul><sub id='C16v8'></sub></form><legend id='C16v8'></legend><bdo id='C16v8'><pre id='C16v8'><center id='C16v8'></center></pre></bdo></b><th id='C16v8'></th></span></q></dt></tr></i><div id='C16v8'><tfoot id='C16v8'></tfoot><dl id='C16v8'><fieldset id='C16v8'></fieldset></dl></div>

              <small id='C16v8'></small><noframes id='C16v8'>

              <legend id='C16v8'><style id='C16v8'><dir id='C16v8'><q id='C16v8'></q></dir></style></legend>
                1. 本文介绍了gpg --passphrase-fd 不适用于 python 3 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下脚本 encrypt_me.py (从另一个帖子修改) 使用 gpg 对自身进行加密并以装甲形式打印出密文.

                  The following script encrypt_me.py (modified from another post) encrypts itself with gpg and prints out the ciphertext in armored form.

                  但是它只适用于 python2.7 而不是 python3?你知道它在 python3 上运行时出了什么问题吗?

                  However it only works on python2.7 but not python3? Do you have any idea what's wrong when it's running on python3?

                  import subprocess
                  import shlex
                  import os
                  import sys
                  
                  in_fd, out_fd = os.pipe()
                  passphrase = 'passsssphrase'
                  os.write(out_fd, passphrase.encode('utf-8'))
                  os.close(out_fd)
                  cmd = 'gpg --passphrase-fd {fd} -c --armor'.format(fd=in_fd)
                  
                  with open(__file__,'r') as stdin_fh:
                      proc=subprocess.Popen(shlex.split(cmd),
                                            stdin=stdin_fh,
                                            stdout=sys.stdout)
                      proc.communicate()
                  
                  os.close(in_fd)
                  

                  用python2.7:

                  With python2.7:

                  $ python encrypt_me.py
                  Reading passphrase from file descriptor 3    
                  -----BEGIN PGP MESSAGE-----
                  Version: GnuPG v1.4.12 (GNU/Linux)
                  
                  jA0EAwMCXrbnOPX+CipgycBD3ErAKmba6UvtA35mjomOlbiOHX2M0bULbV+v8q8U
                  AJ+sTQcFZK+NoauMgUFm39/ZcNoI7W5u78x8dj5B1N6jLk11C7MgmkNmT5CiliQO
                  kl/el0fDAMnksrqGFpUC6+4ECOTJPpj0Z/Cn/3/62kLHkkbAxs+wyS8lGxXEIEKH
                  XFl3OLRlVmCbvtwzrNMFLiD/St6NHu3Wh9S2xt8fe0PAEAZoYlWWx8lnEQEKewq9
                  EzLlkLldZaDNja3ePzWZ8Z6AeDtowBa8kj+8x/HjxfKLGheBBNQuaeBdcSHgE/OW
                  esS/tEesQUlfUgqrZc2uBalLTV9xwyIpcV4cg8BubPWFCcBrDQ==
                  =iziW
                  -----END PGP MESSAGE-----
                  

                  使用python3:

                  $ python3 encrypt_me.py
                  Reading passphrase from file descriptor 3 ...
                  
                  gpg: error creating passphrase: invalid passphrase
                  gpg: symmetric encryption of `[stdin]' failed: invalid passphrase
                  

                  推荐答案

                  close_fds=True on Python 3 的 POSIX 系统.使用 pass_fds 传递输入管道文件描述符:

                  close_fds=True on POSIX systems on Python 3. Use pass_fds to pass input pipe file descriptor:

                  #!/usr/bin/env python3
                  import os
                  import shlex
                  import sys
                  from subprocess import Popen
                  
                  
                  passphrase = 'passsssphrase'
                  file_to_encrypt = sys.argv[1] if len(sys.argv) > 1 else 'encrypt_me.py'
                  
                  in_fd, out_fd = os.pipe()
                  cmd = 'gpg --passphrase-fd {fd} -c --armor -o -'.format(fd=in_fd)
                  with Popen(shlex.split(cmd) + [file_to_encrypt], pass_fds=[in_fd]):
                      os.close(in_fd) # unused in the parent
                      with open(out_fd, 'w', encoding='utf-8') as out_file:
                          out_file.write(passphrase)
                  

                  您也可以通过标准输入传递密码:

                  You could also pass the passphrase via stdin:

                  #!/usr/bin/env python3
                  import sys
                  from subprocess import PIPE, Popen
                  
                  
                  passphrase = 'passsssphrase'
                  file_to_encrypt = sys.argv[1] if len(sys.argv) > 1 else __file__
                  
                  cmd = 'gpg --passphrase-fd 0 -c --armor -o -'.split()
                  with Popen(cmd + [file_to_encrypt], stdin=PIPE) as process:
                      process.stdin.write(passphrase.encode('utf-8'))
                  

                  这篇关于gpg --passphrase-fd 不适用于 python 3 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python子进程超时终止 下一篇:我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?

                  相关文章

                  1. <i id='OuTen'><tr id='OuTen'><dt id='OuTen'><q id='OuTen'><span id='OuTen'><b id='OuTen'><form id='OuTen'><ins id='OuTen'></ins><ul id='OuTen'></ul><sub id='OuTen'></sub></form><legend id='OuTen'></legend><bdo id='OuTen'><pre id='OuTen'><center id='OuTen'></center></pre></bdo></b><th id='OuTen'></th></span></q></dt></tr></i><div id='OuTen'><tfoot id='OuTen'></tfoot><dl id='OuTen'><fieldset id='OuTen'></fieldset></dl></div>

                    <tfoot id='OuTen'></tfoot>

                    • <bdo id='OuTen'></bdo><ul id='OuTen'></ul>
                  2. <legend id='OuTen'><style id='OuTen'><dir id='OuTen'><q id='OuTen'></q></dir></style></legend>

                    <small id='OuTen'></small><noframes id='OuTen'>