如何在 Python 中打印出 http-response 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37616460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:39:23  来源:igfitidea点击:

How to print out http-response header in Python

pythonhttpheaderresponse

提问by Naomi

Today I actually needed to retrieve data from the http-header response. But since I've never done it before and also there is not much you can find on Google about this. I decided to ask my question here.

今天我实际上需要从 http-header 响应中检索数据。但是因为我以前从未做过,而且你在谷歌上也找不到太多关于这方面的信息。我决定在这里问我的问题。

So actual question: How does one print the http-header response data in python? I'm working in Python3.5 with the requests module and have yet to find a way to do this.

所以实际问题:如何在python中打印http-header响应数据?我正在使用请求模块在 Python3.5 中工作,但还没有找到一种方法来做到这一点。

采纳答案by Dilettant

Update: Based on comment of OP, that only the response headers are needed. Even more easy as written in below documentation of Requests module:

更新:根据 OP 的评论,只需要响应头。更简单的写在下面的请求模块文档中:

We can view the server's response headers using a Python dictionary:

我们可以使用 Python 字典查看服务器的响应头:

>>> r.headers
{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'
}

And especially the documentation notes:

尤其是文档说明:

The dictionary is special, though: it's made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

So, we can access the headers using any capitalization we want:

不过,这本字典很特别:它只是为 HTTP 标头而制作的。根据 RFC 7230,HTTP 标头名称不区分大小写。

因此,我们可以使用任何我们想要的大小写来访问标题:

and goes on to explain even more cleverness concerning RFC compliance.

并继续解释有关 RFC 合规性的更多聪明之处。

The Requests documentationstates:

请求的文件中指出:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly. When streaming a download, the above is the preferred and recommended way to retrieve the content.

使用 Response.iter_content 将处理许多您在直接使用 Response.raw 时必须处理的内容。流式下载时,以上是检索内容的首选和推荐方式。

It offers as example:

它提供了以下示例:

>>> r = requests.get('https://api.github.com/events', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

But also offers advice on how to do it in practice by redirecting to a file etc. and using a different method:

但也提供了如何通过重定向到文件等并使用不同的方法在实践中做到这一点的建议:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly

使用 Response.iter_content 将处理很多你在直接使用 Response.raw 时必须处理的事情

回答by NepCoder

How about something like this:

这样的事情怎么样:

import urllib2
req = urllib2.Request('http://www.google.com/')
res = urllib2.urlopen(req)
print res.info()
res.close();

If you are looking for something specific in the header:

如果您正在寻找标题中的特定内容:

For Date: print res.info().get('Date')

回答by Josh Correia

Here's how you get justthe response headers using the requests library like you mentioned (implementation in Python3):

这里是你如何得到只是使用请求库就像你提到的(在Python3执行)的响应头:

import requests

url = "https://www.google.com"
response = requests.head(url)
print(response.headers) # prints the entire header as a dictionary
print(response.headers["Content-Length"]) # prints a specific section of the dictionary

It's important to use .head() instead of .get() otherwise you will retrieve the whole file/page like the rest of the answers mentioned.

使用 .head() 而不是 .get() 很重要,否则您将像提到的其余答案一样检索整个文件/页面。

If you wish to retrieve a URL that requires authentication you can replace the above responsewith this:

如果您希望检索需要身份验证的 URL,您可以将上述内容替换为response

response = requests.head(url, auth=requests.auth.HTTPBasicAuth(username, password))

回答by Kevin Liu

I'm using the urllib module, with the following code:

我正在使用 urllib 模块,代码如下:

from urllib import request
with request.urlopen(url, data) as f:
    print(f.getcode())  # http response code
    print(f.info())     # all header info

    resp_body = f.read().decode('utf-8') # response body

回答by Utkarsh Agrawal

Try to use req.headersand that's all. You will get the response headers ;)

尝试使用req.headers,仅此而已。您将获得响应标头;)

回答by Ahmed

easy

简单

import requests

site = "https://www.google.com"
headers = requests.get(site).headers
print(headers)

if you want something specific

如果你想要一些特定的东西

print(headers["domain"])