【问题标题】:Error in python script running in xampp windows在 xampp windows 中运行的 python 脚本出错
【发布时间】:2013-08-30 10:25:42
【问题描述】:

我是 PYTHON 的新手,通常使用 PHP 编写代码。这是我尝试在 Windows XAMPP 上运行的第一个脚本。我为 .py 启用了 addhandler 并尝试运行以下脚本:

#!C:\Python33\python.exe
# enable debugging
import cgitb
cgitb.enable()
print ("Content-Type: text/html;charset=utf-8")
print ("Hello World!")

运行代码时出现以下错误:

服务器遇到内部错误,无法完成您的请求。

错误信息: 来自脚本“test.py”的错误标头:错误标头:Hello World!

如果您认为这是服务器错误,请联系网站管理员。

【问题讨论】:

  • 尽量不要重新发明轮子——我建议你使用框架而不是手工操作。 flask 是一个很棒的轻量级框架。

标签: python python-3.x cgi


【解决方案1】:

您应该将标题与正文分开打印附加换行符:

#!C:\Python33\python.exe
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print() # <----------- addtional newlnie for header/body separation.
print("Hello World!")

【讨论】:

    【解决方案2】:

    对于仍然遇到错误的任何人,请尝试按以下方式将 print() 替换为 print("\r\n"):

    print("Content-Type: text/html;charset=utf-8")
    print("\r\n")
    print("Hello World!")
    

    这应该可以!

    【讨论】:

      猜你喜欢
      • 2014-04-19
      • 2019-08-21
      • 2017-07-30
      • 2017-12-25
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2020-07-30
      • 2013-10-07
      相关资源
      最近更新 更多