gpt4 book ai didi

php - 两个脚本之间的通信

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:07 24 4
gpt4 key购买 nike

我正在使用无限的Python脚本(启动时启动)和PHP网页创建嵌入式系统。 PHP 网页必须与该脚本进行通信。

当前解决方案是基于文件的通信。 PHP 将一些命令写入文件,Python 将响应写入其他(或相同)文件。

另一种可能的解决方案是仅在需要时使用 PHP 调用一些非无限的 Python 脚本 $response = shell_exec('./script.py');

这两种解决方案都是可能的,但它们都很复杂,而且我需要 Python 脚本是无限的。

有什么方法可以在同一 Linux 设备上的两个独立的 scipt 之间打开一些通信隧道吗?

例如两个设备之间的 UART、telnet 等。

现在我正在寻找 PHP-PYTHON 和 PYTHON-PYTHON 通信的解决方案,但有时我需要这个,例如使用 Bash、TCL 等。

谢谢(很抱歉英语不好)

拉迪姆

最佳答案

在不了解你想要实现的目标的情况下,我会选择一个基本的http解决方案,用python启动一个简单的http服务器

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

然后php脚本可以简单地直接向python发出一些GET/POST/等请求,并获取http响应,例如通过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);

当然,这是一个简单的 stub ,应该稍微调整一下,但它应该为您提供一个起点。

关于php - 两个脚本之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52924961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com