python 获取响应头

import httplib
def check_web_server(host,port,path):
    h=httplib.HTTPConnection(host,port)
    h.request('GET',path)
    resp = h.getresponse()
    print  'status  =',  resp.status
    print  'reason  =',  resp.reason
    print 'HTTP Headers:'
    for hdr in resp.getheaders():
        print  ' %s: %s' % hdr
check_web_server('www.python.org','80','/')


C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a1.py
status  = 301
reason  = Moved Permanently
HTTP Headers:
 content-length: 0
 via: 1.1 varnish
 x-cache: HIT
 accept-ranges: bytes
 x-timer: S1505804396.056155,VS0,VE0
 strict-transport-security: max-age=63072000; includeSubDomains
 server: Varnish
 retry-after: 0
 connection: close
 x-served-by: cache-nrt6134-NRT
 x-cache-hits: 0
 location: https://www.python.org/
 date: Tue, 19 Sep 2017 06:59:56 GMT

Process finished with exit code 0

原文地址:https://www.cnblogs.com/hzcya1995/p/13349533.html