簡體   English   中英

Python CGI-標頭之前的腳本輸出結束

[英]Python CGI - End of script output before 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

誰能幫我嗎? 提前致謝!

您在輸出文件中缺少</html>標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM