Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在os.system之后清除Python CGI中的stdout_Python_Apache_Svg_Cgi - Fatal编程技术网

在os.system之后清除Python CGI中的stdout

在os.system之后清除Python CGI中的stdout,python,apache,svg,cgi,Python,Apache,Svg,Cgi,我使用os.system调用一个外部程序(apachebatik),该程序将svg数据转换为图像。然后我想下载用户磁盘中的图像 但是每当我用Apache调用cgi时,我都会得到错误: malformed header from script. Bad header=About to transcode 1 SVG file 这是os.system命令中的stdout,因此在搜索之后,我发现我可以使用sys.stdout.flush()来解决问题,但不幸的是,它仍然给出相同的错误。以下是脚本:

我使用os.system调用一个外部程序(apachebatik),该程序将svg数据转换为图像。然后我想下载用户磁盘中的图像

但是每当我用Apache调用cgi时,我都会得到错误:

 malformed header from script. Bad header=About to transcode 1 SVG file
这是os.system命令中的stdout,因此在搜索之后,我发现我可以使用
sys.stdout.flush()
来解决问题,但不幸的是,它仍然给出相同的错误。以下是脚本:

import os
import cgi
import sys
arg = cgi.FieldStorage()
os.system('java -Djava.awt.headless=true -jar "batik-1.7/batik-rasterizer.jar" "pythonchart.svg"')
sys.stdout.flush() //NOT WORKING, STILL THE SAME ERROR

print "Content-Type: image/png"
print "Content-Disposition: attachment; filename=pythonchart.png"
print
print open("pythonchart.png","rb").read()
考虑改用
Popen
。 一个明显的好处是,您可以以一种更整洁的方式控制stdout/stdin,而不会“流血”到主程序中。其次,如果需要,您可以向外部应用程序发送输入

from subprocess import Popen, STDOUT, PIPE
from time import sleep

x = Popen('external-command -test paramater', shell=True, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
while x.poll() == None:
    sleep(0.025)
x.stdout.close()
x.stdin.close()