donglianjiang9321 2018-10-22 08:16 采纳率: 0%
浏览 286

两个脚本之间的通信

i'm creating embedded system with infinite Python script (launched on startup) and PHP web page. PHP web page have to communicate with that script.

Current solution is file based communication. PHP writes to file some command and Python writes response to some other (or same) file.

Another possible solution is to call some NOT infinite Python script only at need with PHP $response = shell_exec('./script.py');

Both solutions are possible but they are complicated and I need that Python script to be infinite.

Is there any way how to open some communication tunnel between two independents scipts on same linux device?

Like UART, telnet, etc. between two devices.

Now i'm searching for solution with PHP-PYTHON and PYTHON-PYTHON communication but sometimes i need this for example with Bash, TCL, etc.

Thank you (and sorry for not good english)

Radim

  • 写回答

1条回答 默认 最新

  • dongtui6347 2018-10-22 08:41
    关注

    Without knowing much about what you are trying to achieve, i would go for a base http solution, starting a simple http server with python

    import SimpleHTTPServer
    import SocketServer
    
    PORT = 8000
    
    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    
    .... do something with the http request and send the response
    

    Then the php script can simply make some GET/POST/etc.. request to python directly, and grab the http responses, for example via cURL:

    // create curl resource
    $ch = curl_init();
    
    // set url
    curl_setopt($ch, CURLOPT_URL, "localhost");
    
    // set port
    curl_setopt($ch, CURLOPT_PORT, 8000);
    
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // $output contains the output string
    $output = curl_exec($ch);
    
    // close curl resource to free up system resources
    curl_close($ch);
    

    Of course this is a simple stub and it should be tweaked a bit, but it should give you a starting point.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题