所有的框架請求回應都基於一個原則 http請求 --> wsgi伺服器 --> wsgi介面(實際上是框架中自訂實現的函數經過底層封裝) --> 回應 可以參考廖雪峰的教程中關於wsgi介面的講解
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,優化資料表現形式
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'] = urlraise 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 = guesselif mimetype: response.content_type = mimetypeelif mimetype: response.content_type = mimetype#设置Content_typestats = 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_sizeif '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'] = tsraise BreakTheBottle(open(filename, 'r'))上面的三個函數分別實現了,伺服器內部錯誤、重定向、以及檔案下載。檔案下載這個函數實現了,檔案類型的判斷,Content_type的設定、檔案權限的判斷、檔案狀態的取得等。這個函數還是很簡單,可以做客製化。
以上是Bottle源碼之HeaderDict詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!