You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python注释修改引发Apache MIME类型变化的原因咨询

为什么修改Python CGI脚本的注释会影响Apache返回的Content-Type?

我有一个基于Python 3.9.21的简单CGI脚本:

#!venv/bin/python

# " xxx "

def main():
    html = """

<!DOCTYPE html>
<html>
</html>
"""
    print(html)
    exit(0)

if __name__ == "__main__": main()

使用curl访问时,返回的Content-Type为text/html; charset=UTF-8:

$ curl -v http://.../gordo.py
*   Trying 10.1.31.11:80...
* Connected to ... (10.1.31.11) port 80 (#0)
> GET /gordo.py HTTP/1.1
> Host: ...
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 22 May 2025 03:11:21 GMT
< Server: Apache/2.4.62 (CentOS Stream) OpenSSL/3.5.0
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
< 

<!DOCTYPE html>
<html>
</html>

* Connection #0 to host ... left intact

但将注释中的xxx改为the后,脚本变为:

#!venv/bin/python

# " the "

def main():
    html = """

<!DOCTYPE html>
<html>
</html>
"""
    print(html)
    exit(0)

if __name__ == "__main__": main()

此时用curl访问,返回的Content-Type变成了text/plain; charset=UTF-8:

$ curl -v http://.../gordo.py
*   Trying 10.1.31.11:80...
* Connected to ... (10.1.31.11) port 80 (#0)
> GET /gordo.py HTTP/1.1
> Host: ...
> User-Agent: curl/7.76.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Thu, 22 May 2025 03:18:58 GMT
< Server: Apache/2.4.62 (CentOS Stream) OpenSSL/3.5.0
< Transfer-Encoding: chunked
< Content-Type: text/plain; charset=UTF-8
< 

<!DOCTYPE html>
<html>
</html>

* Connection #0 to host ... left intact

我打算手动设置Content-Type,但想搞明白:为什么仅仅修改注释行的内容,会影响Apache对MIME类型的判断?

内容的提问来源于stack exchange,提问作者Gord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:43:12