北漂IT民工 的博客

python得到服务器的http头


import httplib

conn=httplib.HTTPConnection(“www.sina.com”)

conn.request(“GET”, “/“)

r=conn.getresponse()

r.getheaders() #获取所有的http头

r.getheader(“content-length”) #获取特定的头



>>> conn=httplib.HTTPConnection(“www.sina.com.cn”)

>>> conn.request(“GET”, “/“)

>>> r=conn.getresponse()

>>> r.getheaders()

[(‘x-cache’, ‘HIT from sh-9.sina.com.cn’), (‘x-powered-by’, ‘mod_xlayout_jh/0.0.

1vhs.markII.remix’), (‘accept-ranges’, ‘bytes’), (‘expires’, ‘Tue, 25 Mar 2008 0

8:43:33 GMT’), (‘vary’, ‘Accept-Encoding’), (‘server’, ‘Apache/2.0.54 (Unix)’),

(‘last-modified’, ‘Tue, 25 Mar 2008 08:32:57 GMT’), (‘connection’, ‘close’), (‘e

tag’, ‘“b177fb-48d9a-cca4e040”‘), (‘cache-control’, ‘max-age=60’), (‘date’, ‘Tue

, 25 Mar 2008 08:42:33 GMT’), (‘content-type’, ‘text/html’), (‘age’, ‘54’)]

>>> r.getheader(“content-length”)

>>>