2

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

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

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

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

我通过execPHP 调用 python 脚本。但是它不起作用。

exec("python client.py")

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

4

2 回答 2

0

不确定我是否可以帮助你,但这里有一些我会考虑的事情:

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

如果您已经尝试过所有这些事情,我想不出另一种解决方案.. 祝你好运!

于 2013-05-07T12:13:44.240 回答
0

作为起点,您可能会发现以下程序很有帮助。它旨在运行 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;

?>
于 2013-05-07T12:54:43.430 回答