簡體   English   中英

在PHP中執行Python腳本

[英]Executing Python Script in PHP

我使用Python中的twisted庫編寫了一個簡單的聊天程序。 基本上我有一個服務器程序(server.py)和一個聊天程序(client.py)

client.py是一個簡單的python腳本,它將在特定端口上連接到服務器並在終端上打印消息。

我可以在本地系統上運行server.py和client.py,並且可以在不同的終端上聊天。

我想將client.py集成到PHP中,並能夠通過瀏覽器聊天。

我正在通過exec在PHP中調用python腳本。 但是,它不起作用。

exec("python client.py")

有什么想法,如果我想念什么?

不知道我能否為您提供幫助,但是我會考慮以下幾點:

  • 您是否嘗試過執行其他命令,這行得通嗎?
  • 您是否已正確設置.php文件和Python.exe的權限?
  • 您可以從您的PHP文件所在的文件夾的終端窗口(Windows / Mac / Linux)中運行命令嗎?

如果您已經嘗試了所有這些方法,那么我想不出另一種解決方案了。祝您好運!

您可能會發現以下程序對您有所幫助。 它旨在運行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;

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM