CGI脚本在运行时显示源代码

时间:2018-07-09 17:32:12

标签: python mysql xampp cgi

我正在尝试制作一个简单的CGI脚本,该脚本根据某人输入的html表单来显示他们的名字,但是一旦提交表单,它就会显示CGI文件的源代码。

这是html表单的代码:

<!DOCTYPE html>
<html>  
    <head>  
        <title> </title>
    </head>
    <body>
            <form action="processname.cgi" method="post">
                Please enter your name:
                <input type="text" name="firstname" autofocus="autofocus" required="required">
                <br>
                <input type="submit" name="submitname" value="Submit Name!">
            </form>
    </body>
</html>

这是cgi脚本的代码:

#!C:\Users\MyPc\AppData\Local\Programs\Python\Python35\python.exe
import cgi 

def htmlTop():
    print("""Content-type:text/html\n\n
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title>My server side template</title>
    </head>
    <body>""")

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

def getData():
    formData = cgi.FieldStorage()
    firstname = formData.getvalue("firstname")
    return firstname

if __name__ == '__main__':
    try:
        htmlTop()
        firstName = getData()
        print("Hello {0}".format(firstName))
        htmlTail()
    except:
        cgi.print_exception()

任何帮助都需要事先感谢。

1 个答案:

答案 0 :(得分:0)

这意味着您的cgi文件脚本被视为一个简单的文本文件,因此我想您的Web服务器不会将文件内容重定向到要处理的python模块并转换为适当的html。可能是因为Web服务器配置错误,无法使用cgi。

相关问题