简体   繁体   中英

Communication between two scripts

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM