Bottle源码阅读(3) HeaderDict
class HeaderDict(dict):
''' A dictionary with case insensitive (titled) keys.
You may add a list of strings to send multible headers with the same name.'''
def __setitem__(self, key, value):
return dict.__setitem__(self,key.title(), value) #注意这里使用title函数,它能将每个单词的开头大写
def __getitem__(self, key):
return dict.__getitem__(self,key.title())
def __delitem__(self, key):
return dict.__delitem__(self,key.title())
def __contains__(self, key):
return dict.__contains__(self,key.title())
def items(self):
""" Returns a list of (key, value) tuples """
for key, values in dict.items(self):
if not isinstance(values, list):
values = [values]
for value in values:
yield (key, str(value))
def add(self, key, value):
""" Adds a new header without deleting old ones """
if isinstance(value, list):
for v in value:
self.add(key, v) #注意这里使用了递归
elif key in self:
if isinstance(self[key], list):
self[key].append(value)
else:
self[key] = [self[key], value]
else:
self[key] = [value]
HeaderDict封装了dict,并将字典的键的单词的首字母进行大写。并且将value变成一个可迭代的对象,将value变成一个list对象,即value=[value]。WSGI标准中定义了要将一个字符串类型转换成list类型,这样会使其有更好的表现形式。server也可以不用一次全部输出可以用yield进行控制输出,以免一次输出过多。总而言之,这个封装了dict的类就实现了两个功能:
- 将value转换为list,优化数据表现形式
- 将key中单词的首字母大写
def abort(code=500, text='Unknown Error: Appliction stopped.'):
""" Aborts execution and causes a HTTP error. """
raise HTTPError(code, text)
def redirect(url, code=307):
""" Aborts execution and causes a 307 redirect """
response.status = code
response.header['Location'] = url
raise BreakTheBottle("")
def send_file(filename, root, guessmime = True, mimetype = 'text/plain'):
""" Aborts execution and sends a static files as response. """
root = os.path.abspath(root) + '/'
filename = os.path.normpath(filename).strip('/')
filename = os.path.join(root, filename)
#判断文件是否可获得
if not filename.startswith(root): #主目录下的文件不可以下载
abort(401, "Access denied.")
if not os.path.exists(filename) or not os.path.isfile(filename):
abort(404, "File does not exist.")
if not os.access(filename, os.R_OK):
abort(401, "You do not have permission to access this file.")
# 获得文件类型
if guessmime:
guess = mimetypes.guess_type(filename)[0]
if guess:
response.content_type = guess
elif mimetype:
response.content_type = mimetype
elif mimetype:
response.content_type = mimetype
#设置Content_type
stats = os.stat(filename)
# TODO: HTTP_IF_MODIFIED_SINCE -> 304 (Thu, 02 Jul 2009 23:16:31 CEST)
if 'Content-Length' not in response.header:
response.header['Content-Length'] = stats.st_size
if 'Last-Modified' not in response.header:
ts = time.gmtime(stats.st_mtime)
ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", ts)
response.header['Last-Modified'] = ts
raise BreakTheBottle(open(filename, 'r'))
上面的三个函数分别实现了,服务器内部错误、重定向、以及文件下载。文件下载这个函数实现了,文件类型的判断,Content_type的设置、文件权限的判断、文件状态的获得等。这个函数还是很简单,可以做定制。