webpy学习之serving images

来源:互联网 发布:网名生成器软件 编辑:程序博客网 时间:2024/06/06 04:39
</pre><p>1,新建文件目录../serving_images/,在该目录下面新建文件serving_images.py,文件内容如下:</p><p></p><p></p><pre name="code" class="python">mport webimport osurls = (  '/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,即可提供图片下载查看功能。


0 0
原创粉丝点击