注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

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

2009-09-27 10:15:37|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源:http://www.phpchina.com/html/07/n-34807.html

周末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!\n");  
    }  
 
    cerl_thread_init();  
 
    $server = cerl_connect('localhost:8889', 'HashServer');  
    if (!$server)  
        die("ERROR: Connect HashSever failed!\n");  
      
    printf(  
        "+-----------------------------------------------------------\n" .  
        "| put <key> <value> - Put (key, value)\n" .  
        "| get <key> - Get value of a key\n" .  
        "| q - Quit\n" .  
        "+-----------------------------------------------------------\n");  
 
    for (;;)  
    {  
        echo "\n> ";  
        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]\n";  
            else 
                echo "error: $ret[0]\n";  
        }  
        else if ($cmd == 'put' || $cmd == 'p')  
        {  
            $ret = $server->call(HashSever_put, $key, $value);  
            if ($ret[0] == code_ok)  
                echo "ok!\n";  
            else 
                echo "error: $ret[0]\n";  
        }  
        else if ($cmd == 'q')  
        {  
            break;  
        }  
        else 
        {  
            echo "unknown command!\n";  
        }  
    }  
 
    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!\n");
 }

 cerl_thread_init();

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

 for (;;)
 {
  echo "\n> ";
  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]\n";
   else
    echo "error: $ret[0]\n";
  }
  else if ($cmd == 'put' || $cmd == 'p')
  {
   $ret = $server->call(HashSever_put, $key, $value);
   if ($ret[0] == code_ok)
    echo "ok!\n";
   else
    echo "error: $ret[0]\n";
  }
  else if ($cmd == 'q')
  {
   break;
  }
  else
  {
   echo "unknown command!\n";
  }
 }

 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!" 
 
else:  
    print "+-----------------------------------------------------------" 
    print "| put <key> <value> - Put (key, value)" 
    print "| get <key> - Get value of a key" 
    print "| q - Quit" 
    print "+-----------------------------------------------------------" 
 
    while True:  
          
        sys.stdout.write("\n> ")  
 
        input = sys.stdin.readline().split()  
        cmd = input[0]  
 
        if cmd == "get" or cmd == "g":  
            if len(input) < 2:  
                continue 
            key = int(input[1], 10)  
            ret = server.call(HashSever_get, key)  
            if ret[0] == pycerl.code_ok:  
                print "result:", ret[1]  
            else:  
                print "error:", ret[0]  
 
        elif cmd == "put" or cmd == "p":  
            if len(input) < 3:  
                continue 
            key = int(input[1], 10)  
            value = int(input[2], 10)  
            ret = server.call(HashSever_put, key, value)  
            if ret[0] == pycerl.code_ok:  
                print "ok!" 
            else:  
                print "error:", ret[0]  
 
        elif cmd == "q":  
            break 
 
        else:  
            print "unknown command!" 
 
pycerl.thread_term() 
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!"

else:
 print "+-----------------------------------------------------------"
 print "| put <key> <value> - Put (key, value)"
 print "| get <key> - Get value of a key"
 print "| q - Quit"
 print "+-----------------------------------------------------------"

 while True:
  
  sys.stdout.write("\n> ")

  input = sys.stdin.readline().split()
  cmd = input[0]

  if cmd == "get" or cmd == "g":
   if len(input) < 2:
    continue
   key = int(input[1], 10)
   ret = server.call(HashSever_get, key)
   if ret[0] == pycerl.code_ok:
    print "result:", ret[1]
   else:
    print "error:", ret[0]

  elif cmd == "put" or cmd == "p":
   if len(input) < 3:
    continue
   key = int(input[1], 10)
   value = int(input[2], 10)
   ret = server.call(HashSever_put, key, value)
   if ret[0] == pycerl.code_ok:
    print "ok!"
   else:
    print "error:", ret[0]

  elif cmd == "q":
   break

  else:
   print "unknown command!"

pycerl.thread_term()
 

值得注意的是,这里我们没有像C/C++一样,用一个编译器为SDL翻译生成PHP/Python的Proxy代码。这是动态语言的优势,哪怕没有Proxy,其代码仍然看起来相当优雅(不过话又说回来,其实C++也可以做到的,但是略损失些性能)。

  评论这张
 
阅读(584)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017