Pydon';t在pythoncgi中运行
我使用的是CentOs 7和alread安装的Apache2。我尝试用Python3运行cgi脚本。我有一个名为index.py的文件,其代码如下:Pydon';t在pythoncgi中运行,python,apache,cgi,centos7,Python,Apache,Cgi,Centos7,我使用的是CentOs 7和alread安装的Apache2。我尝试用Python3运行cgi脚本。我有一个名为index.py的文件,其代码如下: #!/usr/bin/env python3.7 import platform import pymysql import cgitb cgitb.enable() print ("Content-type: text/html\r\n\r\n") print ("<html>\n<body>") print ("&
#!/usr/bin/env python3.7
import platform
import pymysql
import cgitb
cgitb.enable()
print ("Content-type: text/html\r\n\r\n")
print ("<html>\n<body>")
print ("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\$
connection = pymysql.connect(host='127.0.0.1', user='user', password='password', db='database', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "SELECT * FROM Marken"
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
print(row['ID'], row['markenName'], '</br>')
finally:
connection.close()
print ("Python Script Test Page", platform.python_version())
print ("</div>\n</body>\n</html>")
#/usr/bin/env蟒蛇3.7
导入平台
导入pymysql
进口cgitb
cgib.enable()
打印(“内容类型:text/html\r\n\r\n”)
打印(“\n”)
打印(“
选项+执行CGI
DirectoryIndex.py
AddHandler cgi脚本.py
DocumentRoot/var/www/html/cgi
在apache配置部分中,您没有为python文件定义目录索引。还需要定义DocumentRoot,以便python索引文件能够指向正确的路径。根据文档,您需要将处理程序从目录中删除。
选项+执行CGI
DirectoryIndex.py
AddHandler cgi脚本.py
DocumentRoot/var/www/html/cgi
在apache配置部分中,您没有为python文件定义目录索引。还需要定义DocumentRoot,以便python索引文件能够指向正确的路径。根据文档,您需要将handler从目录中删除。请不要只发布代码作为答案,还要解释代码的作用以及如何解决此问题这就是问题的症结所在。带解释的答案通常质量更高,更有可能吸引更多的选票。这里没有什么要解释的。只需阅读文档如何使用apache2部署python应用程序。没有其他设置是错误的。签出此链接作为解释:-那么你应该在回答中包含这一点,并明确指出配置的哪一部分是错误的,以及哪些更改修复了它。请不要仅将代码作为答案发布,还要提供解释代码的作用以及它如何解决问题。带有解释的答案通常质量较高,并且更容易吸引人t upvows.it只是错误的配置,这里没有什么需要解释的。只需阅读文档如何使用apache2部署python应用程序。它只是错误的设置。签出此链接作为解释:-然后你应该在回答中包括这一点,并明确指出配置的哪些部分错误,以及哪些更改修复了它。
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
<VirtualHost *:80>
<Directory "/var/www/html/cgi">
Options +ExecCGI
DirectoryIndex index.py
</Directory>
AddHandler cgi-script .py
DocumentRoot /var/www/html/cgi
</VirtualHost>