【问题标题】:PHP/AJAX call Python - 403 ForbiddenPHP/AJAX 调用 Python - 403 Forbidden
【发布时间】:2017-12-30 13:40:15
【问题描述】:

大家好,我在通过我的网站调用 python 脚本时遇到问题,无论是使用 PHP 还是 AJAX

我所有的东西,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。我能做些什么来解决它?提前致谢!

如果我没记错的话,警报中的输出应该是“测试”。

【问题讨论】:

  • 您的服务器是否配置为运行 CGI 脚本?默认情况下,请求 Python 文件只会为您获取该文件的内容。见:docs.python.org/2/library/…

标签: javascript php python ajax cgi


【解决方案1】:

我终于让它工作了!我在 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

【讨论】:

    【解决方案2】:

    你不能像这样在旅途中调用脚本,你最多只能得到文件的内容。你需要一个服务器来做......你知道......服务器端的东西,比如处理请求。

    看看Flask 。这是一个用于 python 的小型服务器,设置它不需要时间,一旦你设置它,你就可以通过 Javascript 调用你的脚本。您也可以寻找其他解决方案,但我记得当我需要使用 Python 而不是 PHP 时,Flask 是我的救星!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2015-11-23
      相关资源
      最近更新 更多