我有一个旧的基于 Python 的 Web 表单,我正在更新它以使用 GPG 进行加密,而不是不再支持的 python 包。当通过命令行调用脚本时它工作得很好,但通过网络浏览器和 CGI 出现错误:IOError: [Errno 32] Broken pipe。如果我使用 gnupg 包或尝试通过子进程直接与 gpg 对话,则会发生此错误。
版本:
Python 2.4.1
gnupg 0.2.2 (python GPG wrapper)
Apache/2.2.9
gpg 1.4.9
这是一个简化的脚本:
#!/usr/bin/python
import sys
# send python tracebacks out to the web browser
sys.stderr = sys.stdout
import gnupg
gpg = gnupg.GPG()
gpgkey = 'np'
message = 'Our secret message!'
print "Content-type: text/html\r\n"
print '''<html><head><title>Test GPG access via cgi</title>
</head><body><pre>'''
print 'message in the clear:'
print message
encrypted = str(gpg.encrypt(message, 'np'))
print 'message encrypted:'
print encrypted
print '''</pre></body></html>'''sf
当通过命令行调用上述脚本时,它运行得很好,但是当通过 CGI 调用时,它会产生以下错误:
message in the clear:
Our secret message!
Traceback (most recent call last):
File "/home/dkmaster/www/nickads/secure-cgi/gpgtest.py", line 23, in
encrypted = str(gpg.encrypt(message, 'np'))
File "/home/dkmaster/www/nickads/secure-cgi/gnupg.py", line 517, in encrypt
return self.encrypt_file(StringIO(data), recipients, **kwargs)
File "/home/dkmaster/www/nickads/secure-cgi/gnupg.py", line 467, in encrypt_file
self._handle_io(args, file, result, passphrase=passphrase)
File "/home/dkmaster/www/nickads/secure-cgi/gnupg.py", line 201, in _handle_io
_copy_data(file, stdin)
File "/home/dkmaster/www/nickads/secure-cgi/gnupg.py", line 75, in _copy_data
outstream.write(data)
IOError: [Errno 32] Broken pipe
我还尝试通过子进程而不是 gnupg 模块直接与 GPG 对话。
#!/usr/bin/python
import sys
import subprocess
sys.stderr = sys.stdout
print "Content-type: text/html\r\n"
print '''<html><head><title>Test subprocess via cgi</title>
</head><body><pre>'''
plain_text = 'the quick fox ' * 10
print plain_text
gpgCommand = "/usr/bin/gpg --quiet -a -e -r 'np' "
gpgProcess = subprocess.Popen(
gpgCommand,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True
)
encrypted_text = gpgProcess.communicate(plain_text)[0]
print encrypted_text
同样,这在命令行中可以正常工作,但不能通过 CGI 生成类似错误:
Traceback (most recent call last):
File "/home/dkmaster/www/nickads/secure-cgi/subprocesstest.py", line 20, in
encrypted_text = gpgProcess.communicate(plain_text)[0]
File "/usr/lib/python2.5/subprocess.py", line 670, in communicate
return self._communicate(input)
File "/usr/lib/python2.5/subprocess.py", line 1220, in _communicate
bytes_written = self._write_no_intr(self.stdin.fileno(), buffer(input, input_offset, 512))
File "/usr/lib/python2.5/subprocess.py", line 999, in _write_no_intr
return os.write(fd, s)
OSError: [Errno 32] Broken pipe
那么如何修复 CGI 中的管道呢?