[英]PHP/AJAX call Python - 403 Forbidden
大家好,我在使用PHP或AJAX通過我的網站調用python腳本時遇到問題
我所有的東西,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);
}
});
蟒蛇:
degaps1.py
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html"
print ""
arguments = cgi.FieldStorage()
print "test"
當我使用不帶“ ../”的“ url”時,它正在打印整個代碼,現在我在控制台中收到一條消息... 403 Forbidden。 我該如何解決? 提前致謝!
如果我沒看錯,則警報中的輸出應為“ test”。
您不能像這樣隨時隨地調用腳本,最多可以從中獲得的僅僅是文件的內容。 您需要服務器來做...您知道...服務器端的東西,例如處理請求。
看看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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.