两个现象

正常的http请求:

wang@nb:/tmp$ nc 20.0.8.5 80
GET /abc HTTP/1.0


HTTP/1.1 404 NOT FOUND
Server: nginx/1.0.6
Date: Fri, 10 Aug 2012 04:19:29 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Vary: Accept-Language, Cookie
Content-Language: zh-cn
Set-Cookie: sessionid=981f6af6103261f552fe5a1ce4b5a90d; Path=/

<!DOCTYPE html>
……

如果请求的url不以/开头:

wang@nb:/tmp$ nc 20.0.8.5 80
GET abc HTTP/1.0


<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.0.6</center>
</body>
</html>

注意响应部分没有http头部

如果用httplib来发一个这样的请求,会注意到响应码居然是200,这应该是httplib发现没有响应头,自己补的状态码。

>>> import httplib
>>> conn = httplib.HTTPConnection('20.0.8.5', 80)
>>> conn.request('GET', 'x')
>>> r = conn.getresponse()
>>> r
<httplib.HTTPResponse instance at 0x1442ea8>
>>> r.status
200
>>> r.reason
''

OVER.