2016-01-11 76 views
0

使用matplotlib我一直在努力,但收效甚微运行CGI脚本matplotlib。我正在使用Python3.5。在Python 3 CGI

我在网上找到的大多数参考文献都显示了Python2.x的功能。

#!/usr/bin/python 
import os,sys 
import cgi 
import cgitb 
cgitb.enable() 

import matplotlib 
import matplotlib.pyplot as plt 
import pylab 

matplotlib.use('Agg') 

plt.figure() 
plt.plot([1,2,3]) 

import io 
imgData = io.BytesIO() 

pylab.savefig(imgData, format='png') 

imgData.seek(0) 

print("Content-type: image/png") 
print() 

print(imgData.read()) 

我对Arch Linux的运行Apache 2.4.18,我也得到了以下错误:

The server encountered an internal error and was unable to complete your request. 

Error message: 
End of script output before headers: index.py 

If you think this is a server error, please contact the webmaster. 

我的脚本拥有所有必需的权限来执行。

任何帮助将不胜感激。

更新:我移动了matplotlib.use('Agg')正好在import matplotlib之下,现在它已经超过了服务器头错误。后端得到更早的声明,因此上面的声明不起作用。但是,现在我收到错误:

The image 'http://localhost' cannot be displayed since it contains errors. 

如何正确渲染图像?

+0

你引用'pylab'不导入它。 – jpcgt

+0

导入Pylab,仍然收到相同的错误。 – doberoi96

+0

当你绝对正确的,这似乎并不在这里是主要的问题,因为它是不将其更改为“图像/ PNG”甚至工作。 – doberoi96

回答

0

解决我的问题。

以下是为我工作:

#!/usr/bin/python 
import os,sys 
import cgi 
import cgitb 
cgitb.enable() 

import matplotlib 
matplotlib.use('Agg') 

os.environ['HOME'] = '/tmp' 

import matplotlib.pyplot as plt 

plt.plot([1,2,3]) 

print("Content-type: image/png") 
print() 

plt.savefig(sys.stdout, format='png')