首页 > 编程 > Python > 正文

php通过ice通信引擎调用python服务

2020-03-22 16:30:53
字体:
来源:转载
供稿:网友
  • PHP调用python,有很多种方式,查阅了下,大致有以下方式:

    system、exec、shell_exec、passthrupopen、proc_opensocketppythonPHPRPCpip(python in php)XMLRPC、jsonrpc

    当然,还有ice这种通信组件,今天配置了下ice、然后运行了简单的php和python通信、python和python通信的示例程序。

    下载和安装参见http://www.zeroc.com/download.html的Packages for Ubuntu 13.04 (Raring Ringtail) and 14.04 (Trusty Tahr)和Install instructions for Ubuntu 13.04 (Raring Ringtail)两部分,安装过程只碰到一个问题:E: Unable to locate package db5.3-utils,后来发现是程序包名错了,应该是db5.3-util

    ——————————————————————————

    用python写服务端和客户端程序,通过ice进行通信

    ——————————————————————————

    1、Writing a Slice Definition:hello.ice

    module Demo {    interface Printer {        void printString(string s);    };};

    2、Compiling a Slice Definition for Python

    slice2py hello.ice

    3、Writing a Server in Python:server.py

    import sys, traceback, Iceimport Democlass PrinterI(Demo.Printer):    def printString(self, s, current=None):        print sstatus = 0ic = Nonetry:    ic = Ice.initialize(sys.argv)    adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")    object = PrinterI()    adapter.add(object, ic.stringToIdentity("SimplePrinter"))    adapter.activate()    ic.waitForShutdown()except:    traceback.print_exc()    status = 1if ic:    # Clean up    try:        ic.destroy()    except:        traceback.print_exc()        status = 1sys.exit(status)

    4、Writing a Client in Python:client.py

    import sys, traceback, Iceimport Demostatus = 0ic = Nonetry:    ic = Ice.initialize(sys.argv)    base = ic.stringToProxy("SimplePrinter:default -p 10000")    printer = Demo.PrinterPrx.checkedCast(base)    if not printer:        raise RuntimeError("Invalid proxy")    printer.printString("python client say hello!")except:    traceback.print_exc()    status = 1if ic:    # Clean up    try:        ic.destroy()    except:        traceback.print_exc()        status = 1sys.exit(status)

    5、运行server端

    python server.py

    6、运行client端

    python client.py

    7、服务端运行结果如下图:服务端接收到了客户端消息

    wKiom1NXvoHQkj5-AABJ0ShiwuM853.jpg

    ————————————————————————————

    用上边的hello.ice、server.py

    用php写客户端程序,通过ice进行通信

    ————————————————————————————

    1、Compiling a Slice Definition for PHP

    slice2php hello.ice

    2、Writing a Client in PHP:client.php

    <?phprequire 'Ice.php';require 'hello.php';$ic = null;try{    $ic = Ice_initialize();    $base = $ic->stringToProxy("SimplePrinter:default -p 10000");    $printer = Demo_PrinterPrxHelper::checkedCast($base);    if(!$printer)        throw new RuntimeException("Invalid proxy");    $printer->printString("php client say hello!");}catch(Exception $ex){    echo $ex;}if($ic){    // Clean up    try    {        $ic->destroy();    }    catch(Exception $ex)    {        echo $ex;    }}?>

    3、执行php或访问php

    php -f client.php

    4、服务端运行结果如下图:服务端接收到了客户端消息

    wKiom1NXv-3wQJWFAABk7FPBcH8630.jpg

    参考资料:

    http://www.zeroc.com/download.html

    http://doc.zeroc.com/display/Ice/Writing+a+Slice+Definition

    http://doc.zeroc.com/display/Ice/Writing+an+Ice+Application+with+Python

    http://doc.zeroc.com/display/Ice/Writing+an+Ice+Application+with+PHP

    注:我的环境是ubuntu kylin 13.04/apache2.2.22/python2.7.4/ice3.5

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表