HeaderDict封裝了dict,並將字典的鍵的單詞的首字母進行大寫。並且將value變成一個可迭代的對象,將value變成一個list對象,即value=[value]。WSGI標準中定義了要將一個字元串類型轉換成list類型,這樣會使其有更好的表現形式。server也可以不用一次全部輸出可以用 ...
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的設置、文件許可權的判斷、文件狀態的獲得等。這個函數還是很簡單,可以做定製。