def GET(self, *args):
dir, name = args[-2:]
dir_path = 'templates/%s/' %dir
ext = name.split(".")[-1]
cType = {
"css": "text/css",
"png": "images/png",
"jpg": "images/jpeg",
"gif": "images/gif",
"ico": "images/x-icon",
"js" : "text/javascrip",
}
if name in os.listdir(dir_path):
web.header("Content-Type", cType[ext])
file_path = '%s/%s' %(dir_path, name)
return open(file_path, "rb").read()
else:
raise web.notfound()
python类notfound()的实例源码
def testUnload(self):
x = web.storage(a=0)
urls = (
"/foo", "foo",
"/bar", "bar"
)
class foo:
def GET(self):
return "foo"
class bar:
def GET(self):
raise web.notfound()
app = web.application(urls, locals())
def unload():
x.a += 1
app.add_processor(web.unloadhook(unload))
app.request('/foo')
self.assertEquals(x.a, 1)
app.request('/bar')
self.assertEquals(x.a, 2)
def notfound():
return web.notfound("Sorry, the page you were looking for was not found.")
__init__.py 文件源码
项目:Desert-Fireball-Maintainence-GUI
作者: CPedersen3245
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def not_found():
return web.notfound("The requested file(s) could not be found.")
def notfound():
return web.notfound("Sorry, the page you were looking for was not found.")
# You can use template result like below, either is ok:
#return web.notfound(render.notfound())
#return web.notfound(str(render.notfound()))
def _setReturnCode(self, code):
"""Set the return code
:param: code
:type code: integer or string
returns success: [True|False]
"""
success = False
if code in (200, "200", "ok"):
web.ok()
success = True
elif code in (201, "201", "created"):
web.created()
success = True
elif code in (400, "400", "badrequest"):
web.badrequest()
elif code in (401, "401", "unauthorized"):
web.unauthorized()
elif code in (404, "404", "notfound"):
web.notfound()
elif code in (409, "409", "conflict"):
web.conflict()
elif code in (500, "500", "internalerror"):
web.internalerror()
elif code in (501, "501", "notimplemented"):
# web.notimplemented() # not implemented
# TODO - set 501 code manually
web.internalerror()
if success:
logging.debug("[LayMan][_setReturnCode] Code: '%s'" % code)
else:
logging.error("[LayMan][_setReturnCode] Code: '%s'" % code)
return success
def notfound():
"""Handle 404 not found errors.
Requires `app.notfound = notfound` following definition.
"""
return web.notfound(render.error(404))
def testCustomNotFound(self):
urls_a = ("/", "a")
urls_b = ("/", "b")
app_a = web.application(urls_a, locals())
app_b = web.application(urls_b, locals())
app_a.notfound = lambda: web.HTTPError("404 Not Found", {}, "not found 1")
urls = (
"/a", app_a,
"/b", app_b
)
app = web.application(urls, locals())
def assert_notfound(path, message):
response = app.request(path)
self.assertEquals(response.status.split()[0], "404")
self.assertEquals(response.data, message)
assert_notfound("/a/foo", "not found 1")
assert_notfound("/b/foo", "not found")
app.notfound = lambda: web.HTTPError("404 Not Found", {}, "not found 2")
assert_notfound("/a/foo", "not found 1")
assert_notfound("/b/foo", "not found 2")
def notfound():
return web.notfound(template.errors.generic("The page you were looking for couldn't be found."))
def handler_notfound():
return web.notfound("page not found")
def main():
""" Main setup function """
def urlr(exp):
return virtual_root + exp + "/*"
# Redirect workarounds if enabled
if config.ini.getboolean("cgi", "redirect-workaround"):
os.environ["SCRIPT_NAME"] = ''
os.environ["REAL_SCRIPT_NAME"] = ''
web.config.debug = config.ini.getboolean("cgi", "web-debug-mode")
web.config.session_parameters["timeout"] = config.ini.getint("http", "session-timeout")
web.config.session_parameters["cookie_name"] = config.ini.get("http", "session-cookie-name")
if config.ini.getboolean("cgi", "fastcgi"):
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
steam.api.key.set(config.ini.get("steam", "api-key"))
# Cache file stuff
cache_dir = config.ini.get("resources", "cache-dir")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
urls = (
virtual_root + "api", api_views.subapplication,
urlr("inv/(?:user/)?(.+)"), "optf2.inventory_views.sim_selector",
urlr(""), "optf2.views.index",
urlr("about"), "optf2.views.about",
urlr("(\w+)/items"), "optf2.schema_views.items",
urlr("(\w+)/attributes/?(\d*)"), "optf2.schema_views.attributes",
urlr("(\w+)/particles"), "optf2.schema_views.particles",
urlr("(\w+)/item/(-?\d+)"), "optf2.inventory_views.item",
urlr("(\w+)/item/(\w+)/(-?\d+)"), "optf2.inventory_views.live_item",
urlr("(\w+)/loadout/(\w+)/?(\d*)"), "optf2.inventory_views.loadout",
urlr("(\w+)/feed/(.+)"), "optf2.inventory_views.feed",
urlr("(\w+)/(?:user/)?(.+)"), "optf2.inventory_views.fetch"
)
application = web.application(urls, globals())
application.notfound = notfound
if not config.ini.getboolean("cgi", "web-debug-mode"):
application.internalerror = internalerror
application.add_processor(web.loadhook(lang_hook))
application.add_processor(web.loadhook(motd_hook))
application.add_processor(web.loadhook(conf_hook))
return application