简体   繁体   中英

PHP/AJAX call Python - 403 Forbidden

Hello guys I'm having problems with calling a python script through my website, either using PHP or AJAX

All my stuff, html, php, css and .py are in the same folder / 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"

When I was using the "url" without "../" it was printing the entire code, now I get a message in the console... 403 Forbidden. What can I do to fix it?. Thanks in advance!

If I'm not wrong, the output in the alert should be "test".

You can't just call scripts on the go like this, at most what you can get from this is just the content of the file. You need a server to do... you know... server side stuff like handling requests.

Have a look at Flask . It's a small server for python, it takes no time to set it up and once you set it up you can call your scripts via Javascript. You can also look for other solutions but I remember when I needed to use Python instead of PHP, Flask was my savior!

Finally I got it to work!. I had the configuration on conf.modules.d/apache.conf and conf.d/vhost.conf all well set, with the correct paths to the files... but the main file, conf/httpd.conf had all the settings wrong, I changed them all and it finally worked! :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>

The .py file is in /var/www/cgi-bin/file.py

#!/usr/bin/python
print "Content-type: text/html\n\n";
print "Hello World.\n";

And the ajax is:

$.ajax({
  type: 'POST',
  url: '../cgi-bin/file.py',
  data: {param: param},
  success: function(response){
    output = response;
    alert(output);
  }
});

The output is: alert >> Hello World!

There might be some duplicity but it works now :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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