📄 static.py
字号:
#
# $Workfile: Static.py $ $Revision: 8 $
# $Date: 9/12/01 10:54a $ $Author: Sholden $
#
import Web
from Error import Error
import os
from mimetypes import guess_type
import mx.DateTime as mxdt
import mx.DateTime.ARPA as arpa
class Page(Web.Page):
"""Handle content present as a file in the virtual web filestore.
Note that a whole passel of stuff is inherited, from Web.Page,
much of which is neither required nor used because the content
is simply dragged from a file and squirted down to the client.
"""
def Content(self):
"""Generate static content by squirting file content."""
if self.ReturnCode() == 200: # No error raised
if self.op.upper() != "GET":
raise Error(405, "Method Not Allowed", errmsg="Illegal Operation")
else:
if not self.path:
raise Error(400, "Bad Request", errmsg="Missing file path")
fname = self.path[-1]
ext = os.path.splitext(fname)[1]
mimetype, encoding = guess_type(fname)
if not mimetype:
raise Error(415, "Unsupported Media Type", errmsg="Content type not recognised")
try:
filepath = os.sep.join(self.initpath + self.path)
ims = self.headers.getheader("if-modified-since")
s = os.stat(filepath)
mdt = mxdt.local2gm(mxdt.DateTimeFromTicks(s[8]))
if ims: # optimize if file not modified
sdt = arpa.ParseDateTime(ims.split(";")[0])
# print "If-mod-since:", ims, "Last-mod:", mdt
if mdt <= sdt:
raise Error(304, "Not modified",
errcontent="Not modified")
self.oheaders.append("Last-Modified: %s" % arpa.str(mxdt.DateTimeFromTicks(mdt)))
if encoding:
enchdr = "Content-Encoding: %s\n" % encoding
else:
enchdr = ""
f = open(filepath, "rb")
img = f.read()
return img
except (IOError, OSError):
raise Error(404, "Not Found", errmsg="Content Not Found")
else:
return Web.Page.Content(self) # standard method has smarts we do not
def test():
import rfc822, StringIO, Session
h = rfc822.Message(StringIO.StringIO("""If-Modified-Since: Mon, 09 Sep 2001 09:00:00
"""))
s = Session.Session(1)
p = Page("GET", ["Windows.htm"], {}, s, h, Initpath=["text"])
import sys
sys.stdout.write(p.Generate())
if __name__ == "__main__":
test()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -