当前位置:Gxlcms > Python > Bottle源码之HeaderDict详解

Bottle源码之HeaderDict详解

时间:2021-07-01 10:21:17 帮助过:172人阅读

所有的框架请求响应都基于一个原理 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的类就实现了两个功能:

  1. 将value转换为list,优化数据表现形式

  2. 将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'] = 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详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行