Python CGI脚本未执行

3 投票
2 回答
13963 浏览
提问于 2025-04-16 14:27

我在做一个简单的Python项目,里面有一个HTML文件,看到一个教程教我怎么执行代码。

这是HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title>Admin Login</title>
</head>
<body>
<big><big>Login
Here<br>
<br>
</big></big>
<form action="/var/www/cgi-bin/Login.py" name="LoginForm"><big>Administration
Login<br>
 User Name<br>
  <input name="UserName"><br>
  <br>
  <br>
Password<br>
  <input name="PassWord"><br>
  </big><br>
   <br>
  <br>
<input type="submit">
  <br>
</form>
&nbsp;__&nbsp;<br>
</body>
</html>

还有Python代码:

#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
# get the info from the html form
form = cgi.FieldStorage()
#set up the html stuff
reshtml = """Content-Type: text/html\n
<html>
 <head><title>Security Precaution</title></head>
 <body>
 """

print reshtml 

User = form['UserName'].value
Pass = form['PassWord'].value

if User == 'Myusername' and Pass == 'MyPasword':
    print '<big><big>Welcome'
    print 'Hello</big></big><br>'
    print '<br>'
else:
    print 'Sorry, incorrect user name or password' 

print '</body>'
print '</html>'

问题是,当我提交用户名和密码时,浏览器里只显示了整个代码,而不是我想要的欢迎信息 :(。我用的是Fedora13,有谁能告诉我哪里出错了吗?我甚至还改了文件的权限。

2 个回答

2
<form action="/var/www/cgi-bin/Login.py" name="LoginForm">

试试这个

<form action="/cgi-bin/Login.py" name="LoginForm">

/var/www 可能是你 FTP 网站的路径。网络服务器只会在 /var/www 这个文件夹里查找,所以它会自动把这个部分放在那里。

3

很可能是你的网络服务器没有设置好去执行这个脚本。即使在文件系统中它被标记为“可执行”,这也不一定意味着网络服务器知道它应该执行 .py 文件(而不是直接提供这些文件)。如果你在使用Apache,可以看看这里的内容:http://httpd.apache.org/docs/2.0/howto/cgi.html

撰写回答