我是CGI的初学者,并尝试将输入从HTML传递到python(html文件输入到python文件)。我不知道我在哪里做错了。我正在关注http://www.tutorialspoint.com/python/python_cgi_programming.htm的教程,为了获得额外的帮助,我也查看了Posting html form values to python script,但失败了。我正在使用Windows 8操作系统。
以下是我的代码: 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
注意:我已将两个文件,即test.py和test.html放在&#34; cgi-bin&#34;那是对的吗?因为我来自php,javascript等背景。
如果我做错了,请帮忙吗?
答案 0 :(得分:2)
您已经错误地配置了apache设置(并且您的html文件中有拼写错误)。将HTML文件放在文档根目录中(请参阅docroot设置)和/cgi-bin/
目录中的CGI。
然后在chrome中转到http://localhost/test.html
fyi - HTML文件中的拼写错误无法正确引用您的cgi-bin。第3行缺少正斜杠。它应该是:
<form name="search" action="/cgi-bin/test.py" method="get">
答案 1 :(得分:0)
删除C:\ xampp \ htdocs \中的.htaccess文件 .htaccess文件会自动创建并转到浏览器并刷新它将加载的页面。