홈>
안녕하세요. 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"여야합니다.
- 답변 # 1
- 답변 # 2
마침내 작동했습니다!. 파일에 대한 올바른 경로를 사용하여 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({ type: 'POST', url: '../cgi-bin/file.py', data: {param: param}, success: function(response){ output = response; alert(output); } }); The output is: alert >> Hello World!
중복성이있을 수 있지만 지금 작동합니다 : D
관련 질문
이처럼 이동 중에도 스크립트를 호출 할 수는 없습니다. 대부분 얻을 수있는 것은 파일의 내용 일뿐입니다. 요청 처리와 같은 서버 측 작업을 수행하려면 서버가 필요합니다.
플라스크를 살펴보십시오. 그것은 파이썬을위한 작은 서버이며, 설정하는 데 시간이 걸리지 않으며 일단 설정하면 Javascript를 통해 스크립트를 호출 할 수 있습니다. 다른 솔루션을 찾을 수도 있지만 PHP 대신 Python을 사용해야 할 때 Flask가 구세주였습니다!