PHP/AJAX调用Python - 403个禁止
问题描述:
好家伙我在通过我的网站调用一个python脚本的问题,无论是使用PHP或AJAXPHP/AJAX调用Python - 403个禁止
所有我的东西,HTML,PHP,CSS和的.py在相同的文件夹/ var/www/html/
JS:
document.getElementById('print').onclick = function(){Print()};
function Print() {
var param = 'zya';
$.ajax({
type: 'POST',
url: '../degaps1.py',
data: {param: param},
success: function(response){
output = response;
alert(output);
}
});
的Python:
degaps1.py
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html"
print ""
arguments = cgi.FieldStorage()
print "test"
当我使用没有“../”的“url”时,它会打印整个代码,现在我在控制台中收到一条消息... 403 Forbidden。我能做些什么来解决它?提前致谢!
如果我没有错,警报中的输出应该是“测试”。
答
你不能只是像这样调用脚本,至多你能从中得到的只是文件的内容。你需要一个服务器来做......你知道......服务器端的东西就像处理请求一样。请参阅Flask。这是一个用于python的小型服务器,它不需要任何时间来设置它,一旦你设置了它,你就可以通过Javascript调用你的脚本。您还可以寻找其他解决方案,但我记得当我需要使用Python而不是PHP时,Flask就是我的救星!
答
最后我得到它的工作!我配置conf.modules.d/apache.conf和conf.d/vhost.conf的配置都很好,文件路径正确......但是主文件conf/httpd.conf包含所有设置错了,我改变了他们,终于奏效了! :d
vhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName test
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
ErrorLog /var/www/html/logs/errors.log
CustomLog /var/www/html/logs/access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
的apache.conf
DocumentRoot "/var/www/html"
<IfModule prefork.c>
StartServers 5
MinSpareServers 20
MaxSpareServers 40
MaxRequestWorkers 256
MaxConnectionsPerChild 5500
</IfModule>
的httpd.conf
Include conf.modules.d/*.conf
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
的.py文件是在/ var/WWW /的cgi-bin /file.py
#!/usr/bin/python
print "Content-type: text/html\n\n";
print "Hello World.\n";
而Ajax是:
$.ajax({
type: 'POST',
url: '../cgi-bin/file.py',
data: {param: param},
success: function(response){
output = response;
alert(output);
}
});
The output is: alert >> Hello World!
可能有一些口是心非的,但现在的工作:d
是您的服务器配置为运行CGI脚本?默认情况下,请求一个Python文件只是为了获取该文件的内容。 请参阅:https://docs.python.org/2/library/cgi.html#installing-your-cgi-script-on-a-unix-system – JHS