【问题】
用gevent的pywsgi写的web服务器,在处理头部的时候发现无法直接根据头部字段的名称获取。
【思路】
抠了一下源码,pywsgi在处理http请求的时候,会给除了“Content-Type”和“Content-Length”之外的 Header 字段添加一个 “HTTP_” 前缀。
gevent源码如下所示:
def _headers(self):
key = None
value = None
for header in self.headers.headers:
if key is not None and header[:1] in " \t":
value += header
continue
if key not in (None, 'CONTENT_TYPE', 'CONTENT_LENGTH'):
yield 'HTTP_' + key, value.strip() #### here
key, value = header.split(':', 1)
key = key.replace('-', '_').upper()
if key not in (None, 'CONTENT_TYPE', 'CONTENT_LENGTH'):
yield 'HTTP_' + key, value.strip() #### or here
解决