【问题标题】:Executing Python Script in PHP在 PHP 中执行 Python 脚本
【发布时间】:2013-05-07 11:47:53
【问题描述】:

我使用 Python 中的twisted 库编写了一个简单的聊天程序。基本上我有一个服务器程序(server.py)和一个聊天程序(client.py)

client.py 是一个简单的 python 脚本,它将连接到特定端口上的服务器并在终端上打印消息。

我可以在本地系统上运行server.py和client.py,我可以在不同的终端上聊天。

我想在 PHP 中集成 client.py 并能够通过浏览器聊天。

我在 PHP 中通过 exec 调用 python 脚本。但是它不起作用。

exec("python client.py")

任何想法,如果我错过了什么?

【问题讨论】:

  • 它不工作。”:怎么样?
  • 我想开始用户之间的聊天。如果我手动执行python脚本,我可以做到
  • 你是在windows下还是linux/mac下?在 Windows 情况下,您可以尝试 $tmp = exec(\\python.exe C:\\wamp\\www\\python\\python client.py"
  • 请注意,某些 PHP 环境禁用或限制exec
  • 您知道您的 client.py 可能会阻塞 PHP 进程,该进程迟早会被“webserver”杀死。

标签: php python twisted


【解决方案1】:

不确定我是否可以帮助您,但我会考虑以下几点:

  • 您是否尝试过执行不同的命令,是否有效?
  • 您是否正确设置了.php文件和Python.exe的权限?
  • 您能否从您的 PHP 文件所在文件夹的终端窗口(Windows/Mac/Linux)运行您的命令?

如果您已经尝试过所有这些方法,我想不出其他解决方案。祝您好运!

【讨论】:

    【解决方案2】:

    作为起点,您可能会发现以下程序很有帮助。它旨在运行 Python 程序:

    <?php
    
    // Check that test is not FALSE; otherwise, show user an error.
    function assert_($test)
    {
        if ($test === FALSE)
        {
            echo '<html><head><title>Proxy</title></head><body><h1>Fatal Error</h1></body></html>';
            exit(1);
        }
    }
    
    // Patch this version of PHP with curl_setopt_array as needed.
    if (!function_exists('curl_setopt_array')) {
        function curl_setopt_array($ch, $curl_options)
        {
            foreach ($curl_options as $option => $value) {
                if (!curl_setopt($ch, $option, $value)) {
                    return FALSE;
                }
            }
            return TRUE;
        }
    }
    
    // Fetch the URL by logging into proxy with credentials.
    function fetch($url, $proxy, $port, $user, $pwd)
    {
        $ch = curl_init($url);
        assert_($ch);
        $options = array(
            CURLOPT_PROXY => $proxy . ':' . $port,
            CURLOPT_PROXYAUTH => CURLAUTH_NTLM,
            CURLOPT_PROXYUSERPWD => $user . ':' . $pwd,
            CURLOPT_RETURNTRANSFER => TRUE
        );
        assert_(curl_setopt_array($ch, $options));
        $transfer = curl_exec($ch);
        curl_close($ch);
        assert_($transfer);
        return $transfer;
    }
    
    // Run path with stdin and return program's status code.
    function order($path, $stdin, &$stdout, &$stderr)
    {
        $cmd = './' . basename($path);
        $descriptorspec = array(
            array('pipe', 'r'),
            array('pipe', 'w'),
            array('pipe', 'w')
        );
        $cwd = dirname($path);
        $process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $_REQUEST);
        assert_($process);
        for ($total = 0; $total < strlen($stdin); $total += $written)
        {
            $written = fwrite($pipes[0], substr($stdin, $total));
            assert_($written);
        }
        assert_(fclose($pipes[0]));
        $stdout = stream_get_contents($pipes[1]);
        assert_($stdout);
        assert_(fclose($pipes[1]));
        $stderr = stream_get_contents($pipes[2]);
        assert_($stderr);
        assert_(fclose($pipes[2]));
        return proc_close($process);
    }
    
    // Collect information to run over the proxy.
    @ $user = $_REQUEST['user'];
    @ $pwd = $_REQUEST['pwd'];
    
    // Fetch the URL and process it with the backend.
    $transfer = fetch('http://rssblog.whatisrss.com/feed/', 'labproxy.pcci.edu', 8080, $user, $pwd);
    $status = order('/home/Chappell_Stephen/public_html/backend.py', $transfer, $stdout, $stderr);
    
    // Check for errors and display the final output.
    assert_(strlen($stderr) == 0);
    echo $stdout;
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2012-05-19
      • 2014-12-07
      • 1970-01-01
      相关资源
      最近更新 更多