为什么我不能将 Content-Type:text/html 添加到文本文件中,而 Python 可以?

发布于 2024-12-11 07:21:49 字数 743 浏览 0 评论 0原文

对于大多数人来说,这似乎是一个愚蠢的问题,但为什么我不能这样做:

Content-Type:text/html

<html>
<head><title>Hello</title></head>
<body>
<h1>Hello, World!</h1>
</body>
</html>

在一个名为 test.txt 的文件中,并在浏览器中以 html 形式打开它,而 Python 脚本可以执行此操作:

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

一切正常。我看不出有什么区别,它们都在浏览器请求的信息顶部打印 Content-Type:text/html

This will seem like a stupid question to most of you but why can't I do this:

Content-Type:text/html

<html>
<head><title>Hello</title></head>
<body>
<h1>Hello, World!</h1>
</body>
</html>

in a file called test.txt and open it in my browser as html when a Python script can do this:

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

and everything works fine. I can't see any difference, they are both printing Content-Type:text/html at the top of the information the browser has requested.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-12-18 07:21:49

HTTP 响应由标头和正文组成,由收到的第一个 \r\n\r\n 分隔。当网络服务器发送普通文件时,它已经发送了所有标头和分隔符。
但是,当运行脚本时,不会自动发送标头。

因此,在第一种情况下,您的网络服务器会从扩展中猜测内容类型,并在发送文件内容之前发送适当的标头。在第二个脚本中,脚本发送了内容类型的覆盖标头。然后由网络服务器决定如何处理这个问题,即插入其他标准 http 标头。

编辑:
Apache 使用 mod_mine 和 /etc/mime.types 将文件扩展名映射到内容类型。对于任何没有它理解的扩展名的内容,它可能默认为文本/纯文本。

仅当浏览器没有可使用的 Content-Type 时,浏览器才可能会关闭该扩展。

假定纯文件仅包含数据,并且标头由 Web 服务器生成,而 cgi 脚本则期望/允许生成自己的标头。

An HTTP response consists of headers and body, separate by the first \r\n\r\n received. By the time a web server sends an ordinary file, it has already sent all the headers, and the separator.
When a script is run, however, the headers are not sent automatically.

So in the first case, your webserver guessed the content-type from the extension, and sent appropriate headers, before sending the contents of the file. In the second the script sent an overriding header for the content-type. It's then up to the web-server how it handles this, in terms of inserting the other standard http headers.

EDIT:
Apache use mod_mine and /etc/mime.types to map file extensions to Content-Types. It probably defaults to text/plain for anything that doesn't have an extension it understands.

The browser probably works off the extension only if it doesn't have a Content-Type to use.

Plain files are assumed to just contain the data, and the headers are generated by the web server, whereas cgi scripts are expected/allowed to generate their own headers.

相对绾红妆 2024-12-18 07:21:49

当浏览器直接从文件读取时,它们不会查找 http 标头。当 python 脚本响应浏览器的 http 请求时,情况有所不同,浏览器期望第一行是 http 标头,然后它会按照您的预期进行解释。

When browsers read directly from a file, they aren't looking for http headers. When a python script is answering a browser's http request, that is different, and the browser expects the first lines to be http headers which it then interprets as you would expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文