【问题标题】:Python CGI - End of script output before headersPython CGI - 标头之前的脚本输出结束
【发布时间】:2018-05-30 00:06:34
【问题描述】:

我创建了一个简单的脚本,它可以读取 HTML 文件并解析其中的一些变量。然后它会创建一个新文件并将 HTML 代码写入其中。之后,它应该将您重定向到新创建的文件。但我收到此错误:标题前脚本输出结束。

这是我的 Python 代码:

#!C:\Program Files\Python36\python.exe

import cgi, urllib.request

data = cgi.FieldStorage()

def varordef(name, default):
   return default if data.get(name) == None else data.get(name)

page_title = varordef('page_title', 'Example CGI')

with urllib.request.urlopen('homepage.thtml') as response:
   html = response.read() % (page_title)
   file = open('homepage.html', 'w')
   file.write(html)

print('Content-type: text/html\n\r') # I've already tried removing this line, but that gives me the same error
                                     # And I also tried putting this line on the top, but then I just get a blank page and it doesn't redirect you
                                     # Also, I tried to put both of those headers on the top, and then it redirects me to the specified URL, but the code below wasn't executed and it didn't create the file, so I get a 404


print('Location: homepage.html\n\r')

HTML 文件:

<html>
<head>
    <title>{0}</title>
    <link rel='stylesheet' type='text/css' href=''>
</head>
<body>
    <form action='getfullname.py' method='get'>
        <p id='box-first_name'>
            Please enter your first name:
            <input type='text' id='first_name'>
        </p>
        <p id='box-last_name'>
            Please enter your last name:
            <input type='text' id='last_name'>
        </p>
        <p id='box-full_name'>
            This is your full name:
            <input type='text' id='full_name' disabled>
        </p>
        <p id='box-submit'>
            <input type='submit' id='submit'>
        </p>
    </form>
</body>
</html>

编辑:

这是日志文件:(片段)

[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215: Traceback (most recent call last):\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:   File "Y:/python-web.local/index.py", line 12, in <module>\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:     page_title = varordef('page_title', 'Example CGI')\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:   File "Y:/python-web.local/index.py", line 10, in varordef\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:     return default if data.get(name) == None else data.get(name)\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:   File "C:\\Program Files\\Python36\\lib\\cgi.py", line 585, in __getattr__\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215:     raise AttributeError(name)\r: Y:/python-web.local/index.py
[Fri Dec 15 19:31:04.143105 2017] [cgi:error] [pid 7780:tid 1948] [client 127.0.0.1:50913] AH01215: AttributeError: get\r: Y:/python-web.local/index.py

谁能帮帮我? 提前致谢!

【问题讨论】:

  • 请提供有关错误消息的更多信息,包括它引用的有关错误的代码行。

标签: python html xampp cgi response-headers


【解决方案1】:

您的输出文件中缺少结束 &lt;/html&gt; 标记。

【讨论】:

  • 你的意思是什么文件?什么结束标签?
  • 抱歉,由于格式原因,我的部分答案被隐藏了。您缺少结束 /html 标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 2016-03-24
  • 2021-12-13
  • 2013-12-01
  • 2021-07-14
  • 2017-03-12
相关资源
最近更新 更多