简体   繁体   English

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

[英]Python CGI - End of script output before headers

I've created a simple script that reads an HTML-file and parses some variables on it. 我创建了一个简单的脚本,该脚本读取HTML文件并解析其中的一些变量。 Then it creates a new file and writes the HTML-code to it. 然后,它创建一个新文件并向其中写入HTML代码。 After that, it should redirect you to the newly created file. 之后,它应该将您重定向到新创建的文件。 But I am getting this error: End of script output before headers. 但是我遇到了这个错误:标头之前的脚本输出结束。

This is my Python code: 这是我的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-file: 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>

EDIT: 编辑:

Here is the Log-file: (fragment) 这是日志文件:(片段)

[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

Can anyone help me please? 谁能帮我吗? Thanks in advance! 提前致谢!

您在输出文件中缺少</html>标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM