Python CGI - 标头之前的脚本输出结束

时间:2017-12-15 18:39:19

标签: python html xampp cgi response-headers

我创建了一个简单的脚本,它读取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

有人能帮帮我吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您错过了输出文件中的结束</html>标记。