2013-05-07 117 views
2

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

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

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

我想將client.py整合到PHP中,並能夠通過瀏覽器進行聊天。

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

exec("python client.py") 

任何想法,如果我缺少什麼?

+0

「*它不工作。*」:怎麼樣? – Blender 2013-05-07 11:52:36

+0

我想開始用戶之間的聊天。如果我手動執行python腳本,我可以做到這一點 – Kiran 2013-05-07 11:54:23

+0

你在windows或linux/mac下?在windows的情況下,你可以嘗試$ tmp = exec( \\ python.exe的路徑C:\\ wamp \\ www \\ python \\ python client.py「 – Madthew 2013-05-07 12:26:18

回答

0

不知道如果我可以幫你,但這裏的一些事情,我會考慮:

  • 您是否嘗試過執行不同的命令,運作的?
  • 您是否正確設置了.php文件和Python.exe的權限?
  • 你能從終端窗口(Windows/Mac/Linux)從您的PHP文件所在的文件夾運行命令嗎?

如果你已經嘗試了所有這些東西,我想不出另一種解決方案..祝你好運!

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; 

?>