【发布时间】:2018-12-17 11:09:52
【问题描述】:
我正在尝试制作一个简单的 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()
提前感谢任何帮助。
【问题讨论】:
-
讽刺,是的,但我是认真的:为什么是 CGI?这具有与之相关的巨大风险,并使您的生活比应有的困难得多。除非这是一个纯粹的学术实验,否则有更好的方法来实现这些目标。
-
你说得对,我纯粹是出于学术原因使用它。
-
如果是这种情况,那么您需要了解如何在 CGI 模式下配置您的服务器。获取 XAMPP,大概是使用 Apache
httpd来完成服务部分,这意味着使用httpd配置文件,特别是cgi-binsupport。 -
CGI根据请求启动一个 Python 进程,效率极低。现代 Python 应用程序使用wSGI,它涉及一个可以为许多请求提供服务的持久 Python 进程。 CGI 是第一个在 Web 上运行脚本代码的解决方案(大约 1994 年),它很快就被更好的东西所取代。
-
至于其他风险,您可以从常见的common vulnerabilities 获得零保护,如果您正确使用它,您将 获得任何体面的框架。从学术角度来看,CGI 迫使你以最痛苦的方式吸收近 25 年的安全课程。有了一个框架,您至少可以边学边学,同时仍然编写安全代码。