简体   繁体   English

Python CGI下载文件而不是在CentOS上执行

[英]Python CGI downloads the file instead of executing on CentOS

I have worked on CGI by python programming on Ubuntu and I got satisfied result. 我已经在Ubuntu上通过python编程来开发CGI,并且得到了满意的结果。 I have a HTML file that has a simple form with a simple input as button, and an action to .py file. 我有一个HTML文件,该文件具有简单的形式,带有作为按钮的简单输入以及对.py文件的操作。 the .py file has 755 permission and is located in /var/www/cgi-bin . .py文件具有755权限,位于/var/www/cgi-bin and the HTML is located in /var/www/html . 并且HTML位于/var/www/html I run the CGIHTTPServer by executing the following command in /var/www/ directory. 我通过在/var/www/目录中执行以下命令来运行CGIHTTPServer But when I click the button, the browser instead of executing the .py file, tries to download it and it's annoying me. 但是,当我单击该按钮时,浏览器而不是执行.py文件,而是尝试下载该文件,这很烦我。 I tried to reconfigure http.conf file with <Directory></Directory> tag of cgi-bin many times but no result! 我试图用cgi-bin <Directory></Directory>标记重新配置http.conf文件很多次,但是没有结果! Please HELP ME!!!! 请帮我!!!!

show_database.py 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 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>

Are you running the html file like hhtp://localhost/display_databse.html ? 您是否正在运行像hhtp://localhost/display_databse.html这样的html文件? Well if its still not working you can try XAMPP .Its hassle free to setup the same apache server with mariadb/php for home/testing usage but needs some tweaking for production. 好吧,如果它仍然无法正常工作,您可以尝试XAMPP。它可以轻松地使用mariadb / php设置相同的apache服务器,以用于家庭/测试用途,但需要进行一些调整以进行生产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM