【发布时间】:2014-12-18 14:35:48
【问题描述】:
在 HTTP 请求或响应的标头中,标头键在服务器之间的大小写方面将保持不变。
我问,所以我可以在我的代码中期待:(使用假函数名称)
安全精确的 Python 代码
for hdr in header.keys():
if 'content-length' == hdr.lower():
recv_more_data( header[hdr] ) # header[hdr] == Content-Length (5388) bytes
break # Exit for loop when if statement is met.
我想使用的代码
recv_more_data (header['Content-Length'])
# I know to expect 'Content-Length' not 'content-Length' or some other variation
这意味着服务器将永远返回带有键的标头。
标准要求
GET / HTTP/1.1
Host: www.example-host.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: */*
Accept-Language: en-US
Accept-Encoding: gzip
Connection: closed
Content-Length: 0
一个糟糕但可能的反应?
HTTP/1.1 200 OK
Server: nginx/1.0.15
date: Thu, 23 Oct 2014 00:25:37 GMT
content-Type: text/html; charset=iso-8859-1
transfer-encoding: chunked
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
澄清有助于我的代码整洁。
【问题讨论】:
标签: http http-headers httprequest httpresponse