doushiposong30622 2013-12-06 16:06
浏览 51
已采纳

从PHP发送数据到Python脚本[关闭]

Here is my problem, I have a Python script that will be running indefinitely in a while(1) loop. I want some PHP script to somehow interact with the Python script, and when it does, the script needs to perform a function with the data submitted to the script.

Any ideas would be appreciated!

  • 写回答

5条回答 默认 最新

  • doufu1950 2013-12-06 16:56
    关注

    You can use redis (pub/sub feature for example) as interprocess communication facility.

    Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

    Besides of IPC via redis you can use it as distributed key-value database - big advantage of redis imho.

    Another choice - zeromq - one of most known tools for interprocess communication. There is many tutorials and docs about it on internet. Original guide: http://zguide.zeromq.org/page:all

    ØMQ (also known as ZeroMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fan-out, pub-sub, task distribution, and request-reply. It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPLv3 open source.

    Hello world server and client on python: https://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/patterns/client_server.html

    Simple example of PHP server and python client from official guide:

    PHP server:

    <?php
    /*
    *  Hello World server
    *  Binds REP socket to tcp://*:5555
    *  Expects "Hello" from client, replies with "World"
    * @author Ian Barber <ian(dot)barber(at)gmail(dot)com>
    */
    
    $context = new ZMQContext(1);
    
    //  Socket to talk to clients
    $responder = new ZMQSocket($context, ZMQ::SOCKET_REP);
    $responder->bind("tcp://*:5555");
    
    while (true) {
        //  Wait for next request from client
        $request = $responder->recv();
        printf ("Received request: [%s]
    ", $request);
    
        //  Do some 'work'
        sleep (1);
    
        //  Send reply back to client
        $responder->send("World");
    }
    

    Hello World client in Python:

    #
    #   Hello World client in Python
    #   Connects REQ socket to tcp://localhost:5555
    #   Sends "Hello" to server, expects "World" back
    #
    import zmq
    
    context = zmq.Context()
    
    #  Socket to talk to server
    print "Connecting to hello world server…"
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://localhost:5555")
    
    #  Do 10 requests, waiting each time for a response
    for request in range(10):
        print "Sending request %s …" % request
        socket.send("Hello")
    
        #  Get the reply.
        message = socket.recv()
        print "Received reply %s [ %s ]" % (request, message)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题