当前位置:编程学习 > python >>

PHP或Python调用C++实现的服务器

周末2天,一天写了个php_cerl模块,完成cerl的php client,一天写了个pycerl模块,完成cerl的python client。呵呵,C++的server可以用php或者python来调用了。

对于我们之前用CERL写的HashServer这个服务器(一个很简单的样例,代码参阅:Erlang vs. CERL - CERL简介),其PHP写的Client样例如下:

view plaincopy to clipboardprint?
<?php 
    define(HashSever_put, 1); 
    define(HashSever_get, 2); 
 
    if (!extension_loaded(php_cerl)) { 
        if (!dl(php_cerl. . PHP_SHLIB_SUFFIX)) 
            die("ERROR: Cannot load php_cerl library! "); 
    } 
 
    cerl_thread_init(); 
 
    $server = cerl_connect(localhost:8889, HashServer); 
    if (!$server) 
        die("ERROR: Connect HashSever failed! "); 
     
    printf( 
        "+----------------------------------------------------------- " . 
        "| put <key> <value> - Put (key, value) " . 
        "| get <key> - Get value of a key " . 
        "| q - Quit " . 
        "+----------------------------------------------------------- "); 
 
    for (;;) 
    { 
        echo " > "; 
        list($cmd, $key, $value) = fscanf(STDIN, %s%d%d); 
        if ($cmd == get || $cmd == g) 
        { 
            $ret = $server->call(HashSever_get, $key); 
            if ($ret[0] == code_ok) 
                echo "result: $ret[1] "; 
            else
                echo "error: $ret[0] "; 
        } 
        else if ($cmd == put || $cmd == p) 
        { 
            $ret = $server->call(HashSever_put, $key, $value); 
            if ($ret[0] == code_ok) 
                echo "ok! "; 
            else
                echo "error: $ret[0] "; 
        } 
        else if ($cmd == q) 
        { 
            break; 
        } 
        else
        { 
            echo "unknown command! "; 
        } 
    } 
 
    cerl_thread_term(); 
?>
<?php
 define(HashSever_put, 1);
 define(HashSever_get, 2);

 if (!extension_loaded(php_cerl)) {
  if (!dl(php_cerl. . PHP_SHLIB_SUFFIX))
   die("ERROR: Cannot load php_cerl library! ");
 }

 cerl_thread_init();

 $server = cerl_connect(localhost:8889, HashServer);
 if (!$server)
  die("ERROR: Connect HashSever failed! ");
 
 printf(
  "+----------------------------------------------------------- " .
  "| put <key> <value> - Put (key, value) " .
  "| get <key> - Get value of a key " .
  "| q - Quit " .
  "+----------------------------------------------------------- ");

 for (;;)
 {
  echo " > ";
  list($cmd, $key, $value) = fscanf(STDIN, %s%d%d);
  if ($cmd == get || $cmd == g)
  {
   $ret = $server->call(HashSever_get, $key);
   if ($ret[0] == code_ok)
    echo "result: $ret[1] ";
   else
    echo "error: $ret[0] ";
  }
  else if ($cmd == put || $cmd == p)
  {
   $ret = $server->call(HashSever_put, $key, $value);
   if ($ret[0] == code_ok)
    echo "ok! ";
   else
    echo "error: $ret[0] ";
  }
  else if ($cmd == q)
  {
   break;
  }
  else
  {
   echo "unknown command! ";
  }
 }

 cerl_thread_term();
?>
 

而Python写的Client样例如下:

view plaincopy to clipboardprint?
HashSever_put = 1
HashSever_get = 2
 
import sys, pycerl 
 
pycerl.thread_init() 
 
server = pycerl.connect("localhost:8889", "HashServer") 
if not server: 
    print "ERROR: Connect HashSever failed!" 
 

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,