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
相关产品推荐
相关产品推荐

