您的位置:首页 > 其它

webpy学习之serving images

2015-08-24 11:43 176 查看
</pre><p>1,新建文件目录../serving_images/,在该目录下面新建文件serving_images.py,文件内容如下:</p><p></p><p></p><pre name="code" class="python">mport web
import os

urls = (
'/images/(.*)', 'images' #this is where the image folder is located....
)
class images:
def GET(self,name):
ext = name.split(".")[-1] # Gather extension #<span style="color:#33FF33;">返回获取的所有子字符串</span>

cType = {
"png":"images/png",
"jpg":"images/jpeg",
"gif":"images/gif",
"ico":"images/x-icon"            }

if name in os.listdir('images'):  # Security
web.header("Content-Type", cType[ext]) # Set the Header
return open('images/%s'%name,"rb").read() # Notice 'rb' for reading images
else:
raise web.notfound()
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()


在../serving_images/目录下创建images/目录,将你想要显示的图片放到该目录下,注意文件格式。然后,

python serving_images.py


登陆http://localhost:8080/image_name.png,即可提供图片下载查看功能。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: