简体   繁体   English

获取 Python 以接受 POST / GET 请求

[英]Get Python to accept POST / GET requests

I have a PHP page and a Python program.我有一个 PHP 页面和一个 Python 程序。 The Python program makes an internal API call to get information that is needed to display on the PHP page. Python 程序进行内部 API 调用以获取需要在 PHP 页面上显示的信息。

The Python program is set up to accept an argument, and process the API call using that argument. Python 程序设置为接受一个参数,并使用该参数处理 API 调用。 For example myPythonProgram 12345 - gets information pertaining to account 12345.例如myPythonProgram 12345 - 获取与帐户 12345 相关的信息。

I would like to call this from my PHP page through Ajax, but if I call the program the way it's supposed to be called, I get a 404 (there is a space between the program and the argument).我想通过 Ajax 从我的 PHP 页面调用它,但是如果我按照它应该调用的方式调用程序,我会得到一个 404(程序和参数之间有一个空格)。 I've altered the Python script to accept requests (I think ), but when I do this, the Python code is rendered rather than the result.我已经更改了 Python 脚本以接受请求(我认为),但是当我这样做时,呈现的是 Python 代码而不是结果。 The following fails:以下失败:

$.ajax({
    type: 'POST',
    url: "myPythonProgram",
    data: { id: '12345' },
    success: function(response) {
        // Stuff
    },
    fail: function() {
       alert('Error');
    },
    complete: function() {
        // Stuff
    }
});

I thought that maybe I could call another PHP page to execute the Python program via shell_exec() , but it appears that the page is rendered before the Python program completes its request.我想也许我可以通过shell_exec()调用另一个 PHP 页面来执行 Python 程序,但似乎该页面在 Python 程序完成其请求之前呈现。 Something like:就像是:

$.ajax({
   type: 'POST',
   url: "callPy.php",
   data: { id: '12345' },
   success: function(response) {
       // Stuff
   },
   ....
});

with callPy.php being: callPy.php 是:

<?php

$data = $_REQUEST['data'];

if (isset($data) && $data['id'] != NULL) {
    $info = shell_exec("myPythonProgram " . $data['id']);
    print_r(json_encode($info));
}

?>

If I were to run this exact command myPythonProgram 12345 in a shell, it works.如果我要在 shell 中运行这个确切的命令myPythonProgram 12345 ,它会起作用。 If I modify callPy.php to hard code the id, then call php callPy.php in a shell, it works.如果我修改 callPy.php 以硬编码 id,然后在 shell 中调用php callPy.php ,它就可以工作。 But calling this in a browser renders null.但是在浏览器中调用它会呈现 null。

How can I make python accept a POST or GET request, or make the PHP wait for the Python program to finish before it renders the page?如何让 python 接受 POST 或 GET 请求,或者让 PHP 在呈现页面之前等待 Python 程序完成?

This was resolved by changing the permissions on the files needed for the API call.这是通过更改 API 调用所需文件的权限来解决的。

First, I checked what user was being used for the web browser: ps aux | egrep '(apache|httpd)'首先,我检查了 Web 浏览器使用的是哪个用户: ps aux | egrep '(apache|httpd)' ps aux | egrep '(apache|httpd)' , which showed me that it was 'apache'. ps aux | egrep '(apache|httpd)' ,这告诉我它是 'apache'。 Next, I tried to run the python program as that user: sudo -u apache ./myPythonProgram 12345 .接下来,我尝试以该用户身份运行 python 程序: sudo -u apache ./myPythonProgram 12345 This gave an error on a certificate that was required for the API call.这会导致 API 调用所需的证书出错。

With shell_exec() , PHP will wait for the process to complete.使用shell_exec() ,PHP 将等待进程完成。 Therefore, changing the permissions on that certificate allowed me to run the program as the apache user, and I was able to achieve the desired effect.因此,更改该证书的权限使我能够以apache用户身份运行该程序,并且能够达到预期的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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