📄 static.py
字号:
#
# $Workfile: Static.py $ $Revision: 6 $
# $Date: 8/29/01 4:28a $ $Author: Sholden $
#
import Web
import os
from mimetypes import guess_type
from ErrorPages import Error404Page, Error405Page, Error415Page
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 Generate(self):
"""Generate static content by squirting file content."""
if self.op.upper() != "GET":
return Error405Page(self.op, self.path, self.query,
self.session, self.headers,
oHeaders=self.Headers(), Realm=self.Realm()).Generate()
else:
auth = self.Authenticate()
if auth:
return auth
fname = self.path[-1]
ext = os.path.splitext(fname)[1]
mimetype, encoding = guess_type(fname)
if encoding:
enchdr = "Content-Encoding: %s\r\n" % encoding
else:
enchdr = ""
if not mimetype:
return Error415Page(self.op, self.path,
self.query, self.session, self.headers,
oHeaders=self.Headers(), Realm=self.Realm()).Generate()
try:
filepath = os.sep.join(self.initpath + self.path)
f = open(filepath, "rb")
img = f.read()
return \
"""HTTP/1.0 200 OK Content as requested
Content-Type: %s
Content-Length: %s
%s
%s""" % (mimetype, len(img), enchdr, img)
except IOError:
return Error404Page(self.op, self.path,
self.query, self.session, self.headers,
oHeaders=self.Headers(), Realm=self.Realm()).Generate()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -