我试图通过Python在Apache浏览器中显示图像文件。我使用:
Windows 7的
Apache HTTP服务器2.2
蟒蛇2.7无法在通过Apache服务器提供的网络浏览器中显示图像
配置在httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
RewriteRule ^images/(.+)$ /imagetest.py?img=$1 [QSA,L]
imagetest.py
htdocs文件夹下:
import cgitb; cgitb.enable()
import cgi
form = cgi.FieldStorage()
imgName = form.getvalue("img")
print "Content-type: image/jpeg"
print
print file(r"C:/imageFolder/" + imgName, "rb").read()
# Also tried
# sys.stdout.write("Content-type: image/jpeg\n\n" + file("C:/imageFolder/" + imgName, "rb").read())
# instead of "print"s above
的http://localhost/images/0001.jpg
网址给;
- The image cannot be displayed because it contains errors
Firefox错误,
- 在Chrome上没有任何问题。
但在这两种情况下,http状态码200(使用Firebug查看)获取的图像(大约1 MB)。
有什么建议吗?
此外,这样做的东西是正确的方式吗?我决定使用cgi,因为这台服务器将间接地(即通过python脚本)仅提供图像和一些视频文件。我的意思是,那里没有复杂的操作。但是,该服务器应该能够快速和可靠地处理许多请谢谢。
感谢Ignacio Vazquez-Abrams,我使用mod_xsendfile删除了所有文件IO代码行。 –