简体   繁体   中英

Python CGI Script Error: End of script output before headers

I'm getting an internal server error (500 err) "End of script output before headers" trying to run a cgi using

  • XAMPP Apache on Windows
  • Python 3.3
  • Notepad++ with UNIX Style (\\n) newline chars

My script reads as follows

#!"C:\Python33\python.exe"

import cgi

def htmlTop():
    print("Content-type: text/html")
    print()
    print("""<!DOCTYPE html>
            <html lang="en">
                <head>
                        <meta charset="utf-8"/>
                        <title>My Server Side Test</title>
                </head>
                <body>""")

def htmlTail():
    print("""</body>
        </html>""")

if ___name___ == "__main__":
    try:
        htmlTop()
        print("Hello World")
        htmlTail()
    except:
        cgi.print_exception()

Please note I have tried using print("Content-type: text/html\\n\\n") as opposed to the extra print statement. Thanks!

I know this is an old post but I found the errors in your script


The first mistake I found was the quote you used when requiring python,
so #!"C:\\Python33\\python.exe" should be changed to #!C:\\Python33\\python.exe


The second mistake I found was the additional bars you used
There are 3 bars here->___name___<-and here
so ___name___ should be changed to __name__

So the final code should be

#!C:\Python33\python.exe

import cgi

def htmlTop():
    print("Content-type: text/html")
    print()
    print("""<!DOCTYPE html>
        <html lang="en">
            <head>
                    <meta charset="utf-8"/>
                    <title>My Server Side Test</title>
            </head>
            <body>""")

def htmlTail():
    print("""</body>
    </html>""")

if __name__ == "__main__":
    try:
        htmlTop()
        print("Hello World")
        htmlTail()
    except:
        cgi.print_exception()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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