无法通过 Apache Server 在 Python 提供的 Web 浏览器中显示图像
我正在尝试使用 Python 通过 Apache 服务器在 Web 浏览器中显示图像文件。我正在使用:
视窗 7
Apache http 服务器 2.2
httpd.conf 中的python 2.7
配置:
DocumentRoot "C:/Apache2.2/htdocs"
RewriteRule ^images/(.+)$ /imagetest.py?img=$1 [QSA,L]
htdocs 文件夹下的 imagetest.py
:
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
URL 给出;
- 图像无法显示,因为它包含错误
Firefox 错误,
- Chrome 上没有任何内容。
但在这两种情况下,都以 http 状态代码 200 获取图像(大约 1 MB)(使用 Firebug 查看)。
有什么建议吗?
此外,这样做是正确的方式吗?我决定使用 cgi,因为该服务器除了间接(即通过 python 脚本)提供图像和一些视频文件外,什么也不提供。我的意思是,那里没有复杂的操作。然而,该服务器应该快速可靠地处理许多请求。谢谢。
I am trying to display an image file in web browser via the Apache server by Python. I am using:
windows 7
apache http server 2.2
python 2.7
Configuration in httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
RewriteRule ^images/(.+)$ /imagetest.py?img=$1 [QSA,L]
imagetest.py
under htdocs folder :
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
The http://localhost/images/0001.jpg
URL gives;
- The image cannot be displayed because it contains errors
error at Firefox,
- nothing at Chrome.
But in both cases the image fetched (about 1 MB) with http status code 200 (looked with Firebug).
Any suggestions?
Additionally, is it right way of doing stuff like this way? I decided to use cgi because of this server will serve nothing but images and some video files indirectly (namely via python script). I mean, no complicated operations there. However this server should handle many requests fast and reliable. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
file.read()
不保证读取整个文件。使用shutil.copyfileobj()
进行复制文件内容到sys.stdout
。或者,更好的是,启用并使用 mod_xsendfile。
file.read()
is not guaranteed to read the entire file. Useshutil.copyfileobj()
to copy the file contents tosys.stdout
.Or, better yet, enable and use mod_xsendfile.