2015-11-08 83 views
3

我是CGI的初学者,并试图将输入从HTML传递给python(html文件输入到python文件)。我不知道我在做错什么。我从http://www.tutorialspoint.com/python/python_cgi_programming.htm继续教程和额外的帮助我也看了Posting html form values to python script但失败。我正在使用Windows 8操作系统。服务器过载或CGI脚本中出现错误

这里是我的代码:test.py

#!C:\Python27\python 

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields 
first_name = form.getvalue('first_name') 
last_name = form.getvalue('last_name') 

print "Content-type:text/html\r\n\r\n" 
print "<html>" 
print "<head>" 
print "<title>Hello - Second CGI Program</title>" 
print "</head>" 
print "<body>" 
print "<h2>Hello %s %s</h2>" % (first_name, last_name) 
print "</body>" 
print "</html>" 

的test.html

<html> 
<body> 
<form name="search" action="/cgi-bintest.py" method="get"> 
Search: <input type="text" name="searchbox"> 
<input type="submit" value="Submit"> 
</form> 
</form> 
</body> 
</html> 

在Chrome:当我写"http://localhost/cgi-bin/test.html"它给:

Server error! 

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script. 

If you think this is a server error, please contact the webmaster. 

Error 500 

localhost 
Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12 

注意:我有p这两个文件,即在“cgi-bin”中的test.py和test.html是否正确?因为我来自php,javascript等背景。

如果我做错了,请帮助PLZ吗?

回答

2

您已经错误地配置了您的apache安装程序(并且您的html文件中存在拼写错误)。将HTML文件放入文档根目录(请参阅您的文件根目录设置)和CGI目录中的/cgi-bin/

然后在铬,去http://localhost/test.html

仅供参考 - 在HTML文件错字不能正确地引用您的cgi-bin。第3行缺少正斜杠。它应该是:

<form name="search" action="/cgi-bin/test.py" method="get"> 
+0

你的意思是我shd的地方test.html在htdoc? – user3440716

+0

是文件根? – user3440716

+0

检查你的apache配置。在你的'httpd.conf'中将被设置为'docroot'。这是放置HTML文件的地方。 – user590028

相关问题