Python脚本在本地XAMPP上按原样显示

2024-09-28 23:05:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python在本地服务器(通过XAMPP的Apache服务器)上运行python脚本。最初,脚本显示在文本编辑器上。我使用的代码是-

#!C:\Users\Gaurav Rai\AppData\Local\Programs\Python\Python37-32\python.exe

def htmlTop():
    print("""
           Content-Type: text/html
          <!Doctype html>
          <html lang=en>
                 <head>
                        <meta charset="UTF-8">
                        <title>My First Server Page in Python</title>
                        </head>
                        <body>
            """)

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

if  __name__== "__main__" :
    try:
        htmlTop()
        print("""<h1>Hello  World!</h1>""")
        htmlTail()
    except:
        cgi.print_exception()

即将到来的产出是- enter image description here

在得到-Running Python scripts with Xampp的帮助后

我把密码改成-

def htmlTop():
    print("""Content-Type: text/html""")
    print("""
          <!Doctype html>
          <html lang=en>
                 <head>....(remaining code is same) 

在做了这个更改之后,代码运行良好,并且需要输出“helloworld!”已在本地服务器上获取

有人能解释一下这里到底发生了什么吗


Tags: text服务器脚本langtitledefhtmltype