layout | title |
---|---|
default |
Serving Images |
First let your urls extend beyound images:
import web
urls = (
'/images/(.*)', 'images' #this is where the image folder is located....
)
Now create the class that will handle them:
import os
class images:
def GET(self,name):
ext = name.split(".")[-1] # Gather extension
cType = {
"png":"images/png",
"jpg":"image/jpeg",
"gif":"image/gif",
"ico":"image/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()