[英]Return an image to the browser in python, cgi-bin
我正在嘗試在cgi-bin中設置一個python腳本,它只返回一個帶有content-type:image / png的頭文件並返回圖像。 我嘗試打開圖像並使用print f.read()
返回它,但這不起作用。
編輯:我正在嘗試使用的代碼是:
print "Content-type: image/png\n\n"
with open("/home/user/tmp/image.png", "r") as f:
print f.read()
這是在ubuntu服務器10.04上使用apache。 當我在chrome中加載頁面時,我得到了損壞的圖像圖像,當我在firefox中加載頁面時,我得到The image http://localhost/cgi-bin/test.py" cannot be displayed, because it contains errors.
"rb"
(在基於Windows的環境中,通常就是這種情況。 write
其write
sys.stdout
。 print "Content-type: image/png\\n\\n"
實際打印3個換行符(因為打印自動添加一個“\\ n”到最后。這可能會破壞您的PNG文件。 嘗試:
sys.stdout.write( "Content-type: image/png\r\n\r\n" + file(filename,"rb").read() )
你是否在標題后面加上空白行? 如果沒有,它不是你的標題的結尾!
print 'Content-type: image/png'
print
print f.read()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.