分类: 系统运维
2010-05-24 23:07:39
HEAD方法
(Head方法)要求响应与相应的GET请求的响应一样,但是没有的响应体(response body)。这用来获得响应头(response header)中的元数据信息(meta-infomation)有(很)帮助,(因为)它不需要传输所有的内容
验证方法:
>>> conn = httplib.HTTPConnection('')
>>> conn.request('head', '/index.html')
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> print r1.read()
Traceback (most recent call last):
File "
print r1.read()
File "C:\Python26\lib\httplib.py", line 524, in read
s = self._safe_read(self.length)
File "C:\Python26\lib\httplib.py", line 619, in _safe_read
raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(0 bytes read, 18801 more expected) <====== 此处异常表示没有数据
>>> print r1.getheaders()
[('content-length', '18801'), ('x-cache', 'HIT from xd33-86.HP08040025.sina.com.cn'), ('x-powered-by', 'mod_xlayout_jh/0.0.1vhs.markIII.remix'), ('accept-ranges', 'bytes'), ('expires', 'Tue, 06 Oct 2009 12:49:13 GMT'), ('vary', 'Accept-Encoding'), ('server', 'Apache/2.0.63 (Unix)'), ('last-modified', 'Wed, 16 Sep 2009 09:18:13 GMT'), ('connection', 'close'), ('x-ua-compatible', 'IE=EmulateIE7'), ('cache-control', 'max-age=60'), ('date', 'Tue, 06 Oct 2009 12:48:13 GMT'), ('content-type', 'text/html')]
验证结果:成功
GET方法
(Get方法用来)请求指定的资源。它是目前网上最常用的方法。它不应该用于一些会造成副作用的操作中(在网络应用中用它来提交动作是一种常见的错误用法)
验证方法:
>>> conn = httplib.HTTPConnection('')
>>> conn.request('get', '/index.html')
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> print r1.read()
(打印出 ' 的内容)
验证结果:成功