Django接收自定义http header过程详解
addbyzhj:Django将所有httpheader(包括你自定义的httpheader)都放在了HttpRequest.META这个Python标准字典中,当然HttpRequest.META
中还包含其它一些键值对,这些键值对是Django加进去的,如SERVER_PORT等。对于httpheader,Django进行了重命名,规则如下
(1)所有header名大写,将连接符“-”改为下划线“_”
(2)除CONTENT_TYPE和CONTENT_LENGTH,其它的header名称前加“HTTP_”前缀
参见 https://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest.META
我个人比较喜欢跟踪源代码来查看,源代码如下,
classWSGIRequestHandler(BaseHTTPRequestHandler):
server_version="WSGIServer/"+__version__
defget_environ(self):
env=self.server.base_environ.copy()
env['SERVER_PROTOCOL']=self.request_version
env['REQUEST_METHOD']=self.command
if'?'inself.path:
path,query=self.path.split('?',1)
else:
path,query=self.path,''
env['PATH_INFO']=urllib.unquote(path)
env['QUERY_STRING']=query
host=self.address_string()
ifhost!=self.client_address[0]:
env['REMOTE_HOST']=host
env['REMOTE_ADDR']=self.client_address[0]
ifself.headers.typeheaderisNone:
env['CONTENT_TYPE']=self.headers.type
else:
env['CONTENT_TYPE']=self.headers.typeheader
length=self.headers.getheader('content-length')
iflength:
env['CONTENT_LENGTH']=length
forhinself.headers.headers:
k,v=h.split(':',1)
k=k.replace('-','_').upper();v=v.strip()
ifkinenv:
continue#skipcontentlength,type,etc.
if'HTTP_'+kinenv:
env['HTTP_'+k]+=','+v#comma-separatemultipleheaders
else:
env['HTTP_'+k]=v
returnenv
defget_stderr(self):
returnsys.stderr
defhandle(self):
"""HandleasingleHTTPrequest"""
self.raw_requestline=self.rfile.readline()
ifnotself.parse_request():#Anerrorcodehasbeensent,justexit
return
handler=ServerHandler(
self.rfile,self.wfile,self.get_stderr(),self.get_environ()
)
handler.request_handler=self#backpointerforlogging
handler.run(self.server.get_app())
classWSGIRequest(http.HttpRequest):
def__init__(self,environ):
script_name=base.get_script_name(environ)
path_info=base.get_path_info(environ)
ifnotpath_info:
#SometimesPATH_INFOexists,butisempty(e.g.accessing
#theSCRIPT_NAMEURLwithoutatrailingslash).Wereallyneedto
#operateasifthey'drequested'/'.Notamazinglynicetoforce
#thepathlikethis,butshouldbeharmless.
path_info='/'
self.environ=environ
self.path_info=path_info
self.path='%s/%s'%(script_name.rstrip('/'),path_info.lstrip('/'))
self.META=environ
self.META['PATH_INFO']=path_info
self.META['SCRIPT_NAME']=script_name
self.method=environ['REQUEST_METHOD'].upper()
_,content_params=self._parse_content_type(self.META.get('CONTENT_TYPE',''))
if'charset'incontent_params:
try:
codecs.lookup(content_params['charset'])
exceptLookupError:
pass
else:
self.encoding=content_params['charset']
self._post_parse_error=False
try:
content_length=int(self.environ.get('CONTENT_LENGTH'))
except(ValueError,TypeError):
content_length=0
self._stream=LimitedStream(self.environ['wsgi.input'],content_length)
self._read_started=False
self.resolver_match=None
WSGIRequest类实例化方法__init__(self,environ)中第二个参数就是WSGIRequestHandler.get_environ()方法返回的数据
WSGIRequest.META在environ的基础上加了一些键值对
用Django做后台,客户端向Django请求数据,为了区分不同的请求,想把每个请求类别加在HTTP头部(headers)里面。
先做实验,就用Python的httplib库来做模拟客户端,参考网上写出模拟代码如下:
#coding=utf8
importhttplib
httpClient=None
try:
myheaders={"category":"Books",
"id":"21",
'My-Agent':"Superbrower"
}
httpClient=httplib.HTTPConnection('10.14.1XX.XXX',8086,timeout=30)
httpClient.request('GET','/headinfo/',headers=myheaders)
response=httpClient.getresponse()
printresponse.status
printresponse.reason
printresponse.read()
exceptException,e:
printe
finally:
ifhttpClient:
httpClient.close()
其中'/headinfo/'为服务器的响应目录。
然后是服务端的响应代码,《TheDjangoBook》第七章有个获取META的例子:
#GOOD(VERSION2)
defua_display_good2(request):
ua=request.META.get('HTTP_USER_AGENT','unknown')
returnHttpResponse("Yourbrowseris%s"%ua)
正好看过这个例子,就模拟上面的这个写了一个能够返回客户端自定义头部的模块:
fromdjango.httpimportHttpResponse
defheadinfo(request):
category=request.META.get('CATEGORY','unkown')
id=request.META.get('ID','unkown')
agent=request.META.get('MY-AGENT','unkown')
html="Categoryis%s,idis%s,agentis%s"%(category,id,agent)
returnHttpResponse(html)
运行结果如下:
$pythonget.py #输出: #200 #OK #Categoryisunkown,idisunkown,agentisunkown
可以看到服务器成功响应了,但是却没有返回自定义的内容。
我以为是客户端模拟headers出问题了,查找和试验了许多次都没有返回正确的结果。后来去查Django的文档,发现了相关的描述:
HttpRequest.META
AstandardPythondictionarycontainingallavailableHTTPheaders.Availableheadersdependontheclientandserver,butherearesomeexamples:
- CONTENT_LENGTH–thelengthoftherequestbody(asastring).
- CONTENT_TYPE–theMIMEtypeoftherequestbody.
- HTTP_ACCEPT_ENCODING–Acceptableencodingsfortheresponse.
- HTTP_ACCEPT_LANGUAGE–Acceptablelanguagesfortheresponse.
- HTTP_HOST–TheHTTPHostheadersentbytheclient.
- HTTP_REFERER–Thereferringpage,ifany.
- HTTP_USER_AGENT–Theclient'suser-agentstring.
- QUERY_STRING–Thequerystring,asasingle(unparsed)string.
- REMOTE_ADDR–TheIPaddressoftheclient.
- REMOTE_HOST–Thehostnameoftheclient.
- REMOTE_USER–TheuserauthenticatedbytheWebserver,ifany.
- REQUEST_METHOD–Astringsuchas"GET"or"POST".
- SERVER_NAME–Thehostnameoftheserver.
- SERVER_PORT–Theportoftheserver(asastring).
WiththeexceptionofCONTENT_LENGTHandCONTENT_TYPE,asgivenabove,anyHTTPheadersintherequestareconvertedtoMETAkeysbyconvertingallcharacterstouppercase,replacinganyhyphenswithunderscoresandaddinganHTTP_prefixtothename.So,forexample,aheadercalledX-BenderwouldbemappedtotheMETAkeyHTTP_X_BENDER.
其中红色的部分说明是说除了两个特例之外,其他的头部在META字典中的key值都会被加上“HTTP_”的前缀,终于找到问题所在了,赶紧修改服务端代码:
category=request.META.get('HTTP_CATEGORY','unkown')
id=request.META.get('HTTP_ID','unkown')
果然,执行后返回了想要的结果:
$pythonget.py #正确的输出: #200 #OK #CategoryisBooks,idis21,agentisSuperbrower
得到的经验就是遇到问题要多查文档,搜索引擎并不一定比文档更高效。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。