Python ContentType 和 MimeType 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3452381/
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
What's the difference of ContentType and MimeType
提问by Frangossauro
As far as I know, they are absolute equal. However, browsing some django docs, I've found this piece of code:
据我所知,他们是绝对平等的。但是,浏览一些 django 文档,我发现了这段代码:
HttpResponse.__init__(content='', mimetype=None, status=200, content_type='text/html')
HttpResponse.__init__(content='', mimetype=None, status=200, content_type='text/html')
which surprise me the two getting along each other. The official docs was able to solve the issue in a pratical manner:
这让我感到惊讶的是两人相处得很好。官方文档能够以实用的方式解决这个问题:
content_type is an alias for mimetype. Historically, this parameter was only called mimetype, but since this is actually the value included in the HTTP Content-Type header, it can also include the character set encoding, which makes it more than just a MIME type specification. If mimetype is specified (not None), that value is used. Otherwise, content_type is used. If neither is given, the DEFAULT_CONTENT_TYPE setting is used.
content_type 是 mimetype 的别名。历史上,该参数仅称为 mimetype,但由于这实际上是包含在 HTTP Content-Type 标头中的值,因此它还可以包含字符集编码,这使其不仅仅是一个 MIME 类型规范。如果指定了 mimetype(不是 None),则使用该值。否则,使用 content_type。如果两者都没有给出,则使用 DEFAULT_CONTENT_TYPE 设置。
However, I don't find it elucidating enough. Why we use 2 different naming for (almost the same) thing? Is "Content-Type" just a name used in browser requests, and with very little use outside it?
但是,我觉得它解释得不够清楚。为什么我们对(几乎相同的)事物使用两种不同的命名?“Content-Type”是否只是浏览器请求中使用的名称,而在外部几乎没有使用?
What's the main difference between the each one, and when is right to call something mimetypeas opposed to content-type? Am I being pitty and grammar nazi?
每个人之间的主要区别是什么,什么时候称呼某事mimetype而不是什么是正确的content-type?我是不是很吝啬和语法纳粹?
采纳答案by chryss
Why we use 2 different naming for (almost the same) thing? Is "Content-Type" just a name used in browser requests, and with very little use outside it?
What's the main difference between the each one, and when is right to call something mimetype as opposed to content-type ? Am i being pitty and grammar nazi?
为什么我们对(几乎相同的)事物使用两种不同的命名?“Content-Type”是否只是浏览器请求中使用的名称,而在外部几乎没有使用?
两者之间的主要区别是什么,什么时候将某些东西称为 mimetype 而不是 content-type 是正确的?我是不是很吝啬和语法纳粹?
The reason isn't only backward compatibility, and I'm afraid the usually excellent Django documentation is a bit hand-wavy about it. MIME(it's really worth reading at least the Wikipedia entry) has its origin in extending internet mail, and specifically SMTP. From there, the MIME and MIME-inspired extension design has found its way into a lot of other protocols (such as HTTP here), and is still being used when new kinds of metadata or data need to be transmitted in an existing protocol. There are dozens of RFCs that discuss MIME used for a plethora of purposes.
原因不仅是向后兼容性,而且恐怕通常出色的 Django 文档对此有点手忙脚乱。MIME(至少值得阅读维基百科条目)起源于扩展 Internet 邮件,特别是 SMTP。从那里开始,MIME 和受 MIME 启发的扩展设计已经进入许多其他协议(例如此处的 HTTP),并且在需要在现有协议中传输新类型的元数据或数据时仍在使用。有许多 RFC 讨论了用于多种目的的 MIME。
Specifically, Content-Type:is one among several MIME headers. "Mimetype" does indeed sound obsolete, but a reference to MIME itself isn't. Call that part backward-compatibility, if you will.
具体来说,Content-Type:是几个 MIME 标头之一。“Mimetype”确实听起来过时了,但对 MIME 本身的引用并非如此。如果您愿意,可以将该部分称为向后兼容性。
[BTW, this is purely a terminology problem which has nothing whatsoever to do with grammar. Filing every usage question under "grammar" is a pet peeve of mine. Grrrr.]
[顺便说一句,这纯粹是一个术语问题,与语法无关。在“语法”下提交每个使用问题是我的一个小烦恼。咕噜噜]
回答by Brian S
Why we use 2 different naming for (almost the same) thing?
为什么我们对(几乎相同的)事物使用两种不同的命名?
Backwards compatibility, based on your quote from the documentation.
向后兼容性,基于您在文档中的引用。
回答by Shome Stoned
If you want to know the details see ticket 3526.
欲知详情请看票3526。
Quote:
引用:
Added content_type as an alias for mimetype to the HttpResponse constructor. It's a slightly more accurate name. Based on a patch from Simon Willison. Fully backwards compatible.
将 content_type 作为 mimetype 的别名添加到 HttpResponse 构造函数。这是一个稍微准确的名称。基于 Simon Willison 的补丁。完全向后兼容。
回答by Reggie Carey
I've always viewed contentType to be a superset of mimeType. The only difference being the optional character set encoding. If the contentType does not include an optional character set encoding then it is identical to a mimeType. Otherwise, the mimeType is the data prior to the character set encoding sequence.
我一直认为 contentType 是 mimeType 的超集。唯一的区别是可选的字符集编码。如果 contentType 不包含可选的字符集编码,则它与 mimeType 相同。否则,mimeType 是字符集编码序列之前的数据。
E.G. text/html; charset=UTF-8
例如 text/html; charset=UTF-8
text/htmlis the mimeType;is the additional parameters indicatorcharset=UTF-8is the character set encoding parameter
text/html是 mimeType;是附加参数指标charset=UTF-8是字符集编码参数
E.G. application/msword
例如 application/msword
application/mswordis the mimeType
It cannot have a character set encoding as it describes a well formed octet-streamnot comprising characters directly.
application/msword是 mimeType
它不能有字符集编码,因为它描述了一个octet-stream不直接包含字符的格式良好的字符。

