Django接收自定义http header
原文:http://callmepeanut.blog.51cto.com/7756998/1390769
用Django做后台,客户端向Django请求数据,为了区分不同的请求,想把每个请求类别加在HTTP头部(headers)里面。
先做实验,就用Python的httplib库来做模拟客户端,参考网上写出模拟代码如下:
#coding=utf8 import httplib httpClient = None try: myheaders = { "category": "Books", "id": "21", ‘My-Agent‘: "Super brower" } httpClient = httplib.HTTPConnection(‘10.14.1XX.XXX‘,8086,timeout=30) httpClient.request(‘GET‘,‘/headinfo/‘,headers=myheaders) response = httpClient.getresponse() print response.status print response.reason print response.read() except Exception, e: print e finally: if httpClient: httpClient.close()
其中‘/headinfo/‘为服务器的响应目录。
然后是服务端的响应代码,《The Django Book》第七章有个获取META的例子:
# GOOD (VERSION 2) def ua_display_good2(request): ua = request.META.get(‘HTTP_USER_AGENT‘, ‘unknown‘) return HttpResponse("Your browser is %s" % ua)
正好看过这个例子,就模拟上面的这个写了一个能够返回客户端自定义头部的模块:
from django.http import HttpResponse def headinfo(request): category = request.META.get(‘CATEGORY‘, ‘unkown‘) id = request.META.get(‘ID‘,‘unkown‘) agent = request.META.get(‘MY-AGENT‘,‘unkown‘) html = "<html><body>Category is %s, id is %s, agent is %s</body></html>" % (category, id, agent) return HttpResponse(html)
运行结果如下:
$python get.py #输出: #200 #OK #<html><body>Category is unkown, id is unkown, agent is unkown</body></html>
可以看到服务器成功响应了,但是却没有返回自定义的内容。
我以为是客户端模拟headers出问题了,查找和试验了许多次都没有返回正确的结果。后来去查Django的文档,发现了相关的描述:
- HttpRequest.META
A standard Python dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples:
CONTENT_LENGTH – the length of the request body (as a string).
CONTENT_TYPE – the MIME type of the request body.
HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
HTTP_HOST – The HTTP Host header sent by the client.
HTTP_REFERER – The referring page, if any.
HTTP_USER_AGENT – The client’s user-agent string.
QUERY_STRING – The query string, as a single (unparsed) string.
REMOTE_ADDR – The IP address of the client.
REMOTE_HOST – The hostname of the client.
REMOTE_USER – The user authenticated by the Web server, if any.
REQUEST_METHOD – A string such as "GET" or "POST".
SERVER_NAME – The hostname of the server.
SERVER_PORT – The port of the server (as a string).
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted toMETA keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
其中红色的部分说明是说除了两个特例之外,其他的头部在META字典中的key值都会被加上“HTTP_”的前缀,终于找到问题所在了,赶紧修改服务端代码:
category = request.META.get(‘HTTP_CATEGORY‘, ‘unkown‘) id = request.META.get(‘HTTP_ID‘,‘unkown‘)
果然,执行后返回了想要的结果:
$python get.py #正确的输出: #200 #OK #<html><body>Category is Books, id is 21, agent is Super brower</body></html>
得到的经验就是遇到问题要多查文档,搜索引擎并不一定比文档更高效。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。