gpt4 book ai didi

python - ContentType 和 MimeType 有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 21:09:16 24 4
gpt4 key购买 nike

据我所知,他们是绝对平等的。但是,浏览一些 django 文档,我已经找到了这段代码:

HttpResponse.__init__(content='', mimetype=None, status=200, content_type='text/html')

这让我很惊讶两人相处得很好。官方文档能够以实用的方式解决该问题:

content_type is an alias for mimetype.Historically, this parameter was onlycalled mimetype, but since this isactually the value included in theHTTP Content-Type header, it can alsoinclude the character set encoding,which makes it more than just a MIMEtype specification. If mimetype isspecified (not None), that value isused. Otherwise, content_type is used.If neither is given, theDEFAULT_CONTENT_TYPE setting is used.

但是,我觉得它的解释不够清楚。为什么我们对(几乎相同的)事物使用 2 种不同的命名? “Content-Type”只是浏览器请求中使用的一个名称,在它之外几乎没有使用吗?

两者之间的主要区别是什么,什么时候调用 mimetype 而不是 content-type 是正确的?我是小气和语法纳粹吗?

最佳答案

我一直认为 contentType 是 mimeType 的超集。唯一的区别是可选的字符集编码。如果 contentType 不包含可选的字符集编码,则它与 mimeType 相同。否则,mimeType 是字符集编码序列之前的数据。

例如文本/html; charset=UTF-8

text/html 是 mimeType
;为附加参数指示符
charset=UTF-8为字符集编码参数

例如应用程序/msword

application/msword 是 mimeType
它不能有字符集编码,因为它描述了一个格式良好的 octet-stream 不直接包含字符。

关于python - ContentType 和 MimeType 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3452381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com