我在 Ubuntu 上通过 python 编程进行 CGI 工作,我得到了满意的结果。我有一个 HTML 文件,它有一个简单的表单,带有一个简单的输入作为按钮,以及一个对 .py
的操作文件。 .py
文件有 755
许可,位于 /var/www/cgi-bin
. HTML 位于 /var/www/html
.我运行 CGIHTTPServer
通过在 /var/www/
中执行以下命令目录。但是当我点击按钮时,浏览器并没有执行.py
文件,尝试下载它,这让我很烦。我试图重新配置http.conf
带有 <Directory></Directory>
的文件cgi-bin
的标签多次但没有结果!请帮我!!!!
show_database.py
#!/usr/bin/env python2
import cgi
import cgitb; cgitb.enable()
from MySQLdb import *
# Creates a connection with database
dbconnection = connect(host="localhost", user="root", passwd="", db="testdb")
# Creates a cursor of db
cursor = dbconnection.cursor()
# Selects data from database
cursor.execute("SELECT * FROM inp;")
# HTML view by printing codes as strings
print("Content-type: text/html\r\n\r\n")
print("")
print("<html>")
# Body of HTML Page
print("<body>")
print("<h1>بانک اطلاعاتی!</h1>")
# A table that shows the database
print("<table>")
print("<th>ID</th><th>input</th>")
for row in cursor.fetchall():
print("<tr>")
print("<td>" + str(row['ID']) + "</td>")
print("<td>" + str(row['inpt']) + "</td>")
print("</tr>")
print("</table>")
# End of HTML
print("</body></html>")
display_database.html
<html>
<head>
<meta charset='utf-8' />
</head>
<style>
body { background: #ececec; }
body, p { text-align: center; direction: rtl; }
</style>
<body>
<h1>نمای وبگونه از بانک اطلاعاتی</h1>
<p>دکمه زیر را فشار دهید تا بانک اطلاعاتی را مشاهده نمائید!</p>
<form action="/cgi-bin/show_database.py">
<input type="Submit" value="مشاهدهی بانک" name="data">
</form>
</body>
</html>
最佳答案
你在运行像 hhtp://localhost/display_databse.html
这样的 html 文件吗? ?
好吧,如果它仍然无法正常工作,您可以尝试 XAMPP .它可以轻松设置与 mariadb/php 相同的 apache 服务器用于家庭/测试使用,但需要对生产进行一些调整。
关于Python CGI 下载文件而不是在 CentOS 上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38779463/