簡體   English   中英

Python CGI腳本IOError管道中斷

[英]Python CGI script IOError Broken Pipe

我有一個舊的基於Python的Web表單,我正在對其進行更新以使用GPG進行加密,而不是不再支持的python軟件包。 當通過命令行調用腳本時,它可以正常工作,但是通過Web瀏覽器和CGI,會出現錯誤: IOError:[Errno 32]管道損壞 如果我使用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中修復管道?

我知道答案可能為時已晚,但最近我遇到了同樣的問題,我想我可以解決。

GPG似乎向終端輸出了一些東西(“您需要一個密碼短語” blabla),而不管stdout被重定向了-不要問我如何:)

但是,似乎發生了管道打斷的情況,因為gpg無法在cgi環境中輸出這些消息(使用uwsgi對我來說是偶然的)。 如果--quiet通過了,它甚至可以打印出來。 如果您另外通過--batch它似乎真的真的很安靜。

好問題-我不確定這是python-gnupg還是gpg問題。 這可能與subprocesscgi或兩者之間的某些交互有關。 如果使用最小的腳本(從stdin讀取並將輸出寫入文件)進行嘗試,會發生什么? 那樣有用嗎?

啟用日志記錄以查看拋出的內容(如果有的話)也是值得的。 有關如何執行此操作的示例,請參見test_gnupg.py腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM