码迷,mamicode.com
首页 > 编程语言 > 详细

python+requests——检查响应头是否存在

时间:2020-02-23 20:08:06      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:header   requests   struct   case   lower   bin   cat   access   head   


import requests

resp = requests.get(http://httpbin.org/get)

print(type(resp.headers))

print(resp.headers[Content-Type])

actual_headers = {}


for k,v in resp.headers.items():          #把响应头转为小写
    actual_headers[k.lower()] = v

print(actual_headers)

for k,v in actual_headers.items():
    if k ==content-type:
        print(actual_headers[k])

for k,v in actual_headers.items():
    if k ==content-type and  actual_headers[k] == application/json:
        print(存在)

 

 

 

 

 

执行结果:

 

 

<class ‘requests.structures.CaseInsensitiveDict‘>
application/json
{‘date‘: ‘Sun, 23 Feb 2020 11:21:17 GMT‘, ‘content-type‘: ‘application/json‘, ‘content-length‘: ‘307‘, ‘connection‘: ‘keep-alive‘, ‘server‘: ‘gunicorn/19.9.0‘, ‘access-control-allow-origin‘: ‘*‘, ‘access-control-allow-credentials‘: ‘true‘}
application/json
存在

 

python+requests——检查响应头是否存在

标签:header   requests   struct   case   lower   bin   cat   access   head   

原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12353355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!