你应该
- 首先写入cStringIO对象
- 然后写HTTP头
- 然后将cStringIO的内容写入stdout
因此,如果发生错误
savefig,您仍然可以返回其他内容,甚至另一个标头。有些错误不会更早地识别出来,例如文本问题,图像尺寸太大等。
您需要告诉
savefig将输出写入何处。你可以做:
format = "png"sio = cStringIO.StringIO()pyplot.savefig(sio, format=format)print "Content-Type: image/%sn" % formatmsvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) # Needed this on windows, IISsys.stdout.write(sio.getvalue())
如果要将图像嵌入HTML:
print "Content-Type: text/htmln"print """<html><body>...a bunch of text and html here...<img />...more text and html...</body></html>""" % sio.getvalue().enpre("base64").strip()


