简体   繁体   English

如何从 HTTP 标头响应中解析 Content-Type 的值?

[英]How to parse the value of Content-Type from an HTTP Header Response?

My application makes numerous HTTP requests.我的应用程序发出大量 HTTP 请求。 Without writing a regular expression, how do I parse Content-Type header values?不编写正则表达式,如何解析Content-Type标头值? For example:例如:

text/html; charset=UTF-8

For context, here is my code for getting stuff in the internet:对于上下文,这是我在互联网上获取东西的代码:

from requests import head

foo = head("http://www.example.com")

The output I am expecting is similar to what the methods do in mimetools .我期望的输出类似于mimetools 中的方法。 For example:例如:

x = magic("text/html; charset=UTF-8")

Will output:将输出:

x.getparam('charset')  # UTF-8
x.getmaintype()  # text
x.getsubtype()  # html

requests doesn't give you an interface to parse the content type, unfortunately, and the standard library on this stuff is a bit of a mess.不幸的是, requests没有给你一个解析内容类型的接口,而且这个东西的标准库有点乱。 So I see two options:所以我看到两个选项:

Option 1 : Go use the python-mimeparse third-party library.选项 1 :去使用python-mimeparse第三方库。

Option 2 : To separate the mime type from options like charset , you can use the same technique that requests uses to parse type/encoding internally: use cgi.parse_header .选项 2 :要将 mime 类型与charset选项分开,您可以使用requests用于在内部解析类型/编码的相同技术:使用cgi.parse_header

response = requests.head('http://example.com')
mimetype, options = cgi.parse_header(response.headers['Content-Type'])

The rest should be simple enough to handle with a split :其余的应该足够简单,可以用split处理:

maintype, subtype = mimetype.split('/')

Your question is bit unclear.你的问题有点不清楚。 I assume that you are using some sort of web application framework such as Django or Flask?我假设您正在使用某种 Web 应用程序框架,例如 Django 或 Flask?

Here is example how to read Content-Type using Flask:以下是如何使用 Flask 读取 Content-Type 的示例:

from flask import Flask, request
app = Flask(__name__)

@app.route("/")
def test():
  request.headers.get('Content-Type')


if __name__ == "__main__":
  app.run()

Your response ( foo ) will have a dictionary with the headers.您的响应 ( foo ) 将有一个带有标题的字典。 Try something like:尝试类似:

foo.headers.get('content-type')

Or print foo.headers to see all the headers.或者打印foo.headers以查看所有标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM