美文网首页
Python获取http请求响应头的headers

Python获取http请求响应头的headers

作者: Queenie的学习笔记 | 来源:发表于2021-08-09 19:50 被阅读0次
    1. 测试下载文件的接口时,我要拿到响应头的Content-Type为application/octet-stream;charset=utf-8类型的响应数据,
      之前用的是下面部分的代码,但这段获取响应头的headers的代码不对,获取到的rq.headers是HTTPHeaderDict,不是<class 'requests.models.Response'>,结果取到的Content-Type'一直是application/json;charset=utf-8:
    import urllib3
    
    http = urllib3.PoolManager()
    tm = urllib3.Timeout(connect=1.0, read=3.0)
    rq = http.request(method, url, headers=headers, timeout=tm, retries=5, redirect=4)  #这里的url和method需要自己指定
    # print("响应码:", rq.status)
    # print("响应头部:", rq.headers)
    # print(type(rq.headers))
    contentType = rq.headers.get('Content-Type')
    
    1. 正确的写法
    res = self.run_method.run_main(method,url,params,data_json,headers)
    ContentType = res.headers.get('Content-Type')
    print("响应头的Content-Type格式:", ContentType)
    

    参考来源:https://www.pythonf.cn/read/93693

    相关文章

      网友评论

          本文标题:Python获取http请求响应头的headers

          本文链接:https://www.haomeiwen.com/subject/xkqtbltx.html