Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与Python模块相结合的PHP脚本_Php_Python - Fatal编程技术网

与Python模块相结合的PHP脚本

与Python模块相结合的PHP脚本,php,python,Php,Python,我的问题可能不正确甚至奇怪,但我对这种编程经验非常感兴趣,原因有二: 作为一名PHP开发人员,我应该做好我的工作,这样我就不能那么容易地切换到其他编程语言;然而,有很多事情会给用PHP编写带来很多麻烦 作为一名Python初学者,我已经是这门语言的超级粉丝了,有些事情可以做得更容易,而且,依我看,可以用PHP实现所建议的更合理的方式来做 例如,我一直在用PHP编写一个广播多连接套接字服务器,任何做过类似工作的人都会明白有多少限制会导致这种解决方案——如果客户端刚刚关闭浏览器,则检测断开连接是可怕

我的问题可能不正确甚至奇怪,但我对这种编程经验非常感兴趣,原因有二:

  • 作为一名PHP开发人员,我应该做好我的工作,这样我就不能那么容易地切换到其他编程语言;然而,有很多事情会给用PHP编写带来很多麻烦

  • 作为一名Python初学者,我已经是这门语言的超级粉丝了,有些事情可以做得更容易,而且,依我看,可以用PHP实现所建议的更合理的方式来做

  • 例如,我一直在用PHP编写一个广播多连接套接字服务器,任何做过类似工作的人都会明白有多少限制会导致这种解决方案——如果客户端刚刚关闭浏览器,则检测断开连接是可怕的。查看Python中的广播服务器实现让我感觉更舒服

    此外,还可以考虑可以工作的应用程序,例如,在脱机模式下收集用户输入并稍后将其发送到处理服务器,或者连接到网站的独立应用程序,等等

    在这种情况下,搜索web很糟糕。我所发现的是,它发布的时间太长了,没有很好的文档记录——这可能有一个很好的原因


    我很高兴听到任何关于这个的想法,因为我知道这个想法有点疯狂,似乎没有很多人关心它。

    不久前,我遇到了类似的困境。我找到的解决方案是使用XMLRPC公开python对象和方法,这样我就可以从php脚本中使用它们。在这里,我给你留下了这两个文件

    Python: PHP:

    编辑:添加示例。示例与文档中的示例相同。我只是把它们改短了一点。在
    client.php
    中,我只从python服务器调用
    div
    函数。把其他的加上你自己

    server.py

    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
    
    # Restrict to a particular path.
    class RequestHandler(SimpleXMLRPCRequestHandler):
        rpc_paths = ('/RPC2',)
    
    # Create server
    server = SimpleXMLRPCServer(("localhost", 8000),
                                requestHandler=RequestHandler)
    server.register_introspection_functions()
    
    # Register pow() function; this will use the value of
    # pow.__name__ as the name, which is just 'pow'.
    server.register_function(pow)
    
    # Register a function under a different name
    def adder_function(x,y):
        return x + y
    server.register_function(adder_function, 'add')
    
    # Register an instance; all the methods of the instance are
    # published as XML-RPC methods (in this case, just 'div').
    class MyFuncs:
        def div(self, x, y):
            return x // y
    
    server.register_instance(MyFuncs())
    
    # Run the server's main loop
    server.serve_forever()
    
    client.php

    <html>
    <head><title>xmlrpc</title></head>
    <body>
    <h1>Php - Python - XMLRPC Demo</h1>
    <?php
    
        // Note that the path to xmlrpc.inc file is relative.
        // to this file.
        include("xmlrpc/lib/xmlrpc.inc");
    
        // Params to python function 10 and 5.
        // Build the message you want send.
        // The message takes the function name and the params. See doc for details on how
        // build params, is pretty easy.
        $msg = new xmlrpcmsg( "div", array(new xmlrpcval(10, "int"), new xmlrpcval(5, "int")) );
        // Build a XMLRCP - Client.
        $client = new xmlrpc_client("/RPC2", "localhost", 8000);
        // And send the message.
        $response = $client->send($msg);
    
    
        // From here all should look familier to you.
        if(!$response->faultCode())
        {
            $v=$response->value();
            echo "The result from div is" . htmlspecialchars($v->scalarval());
    
        }
        else
        {
            print "An error occurred: ";
            print "Code: " . htmlspecialchars($r->faultCode())
                . " Reason: '" . htmlspecialchars($r->faultString()) . "'</pre><br/>";
        }
    
    ?>
    <hr/>
    </body>
    </html>
    
    
    xmlrpc
    Php-Python-XMLRPC演示
    发送($msg);
    //从这里开始,你应该更熟悉这里。
    如果(!$response->faultCode())
    {
    $v=$response->value();
    echo“div的结果是”.htmlspecialchars($v->scalarval());
    }
    其他的
    {
    打印“发生错误:”;
    打印“代码:”.htmlspecialchars($r->faultCode())
    “原因:”.htmlspecialchars($r->faultString())。“
    ”; } ?>

    这里的问题是什么?我如何做到这一点-将PHP与Python模块结合起来?如果是的话,我应该考虑一下吗?如果是的话,有没有最好的方法呢?一般来说,只要坚持一个。。。有时你确实想在php中使用一个工具,你可以用python中的
    exec
    来做这件事,你可以用
    os.system
    来做这件事,虽然pip很酷,但我不推荐这样做,因为你只会让其他使用你的代码库的人感到困惑(不管他们是否了解python或php,大多数开发人员实际上都可以做到这一点,但看到他们这样混合在一起最多也会让人困惑)这确实很有趣,但几个小时后,我仍然不太了解如何使用它。您能分享一个从PHP脚本中调用Python方法的简单示例吗?谢谢。这对于在客户机-服务器基础上运行的解决方案可能会很好。而且,将PHP和Python结合在一起使用并没有简单的方法e、 出于显而易见的原因……是的,我在我的问题中提到了这个PiP项目:)无论如何,看起来这就是结局。谢谢你,雷德尔。