Return an image to the browser in python, cgi-bin

拟墨画扇 提交于 2019-11-28 12:40:41
adamk
  1. You may need to open the file as "rb" (in windows based environments it's usually the case.
  2. Simply printing may not work (as it adds '\n' and stuff), better just write it to sys.stdout.
  3. The statement print "Content-type: image/png\n\n" actually prints 3 newlines (as print automatically adds one "\n" in the end. This may break your PNG file.

Try:

sys.stdout.write( "Content-type: image/png\r\n\r\n" + file(filename,"rb").read() )
  1. HTML responses require carriage-return, new-line

Are you including the blank line after the header? If not, it's not the end of your headers!

print 'Content-type: image/png'
print
print f.read()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!