At the moment I am planning a project with the RaspberryPi. Therefore I plan to write a script in Python that runs in the background and reacts to user input (buttons, rotary knob, etc.). Additional to the Python script I have a webinterface with PHP under it. The goal is to lat the user change settings through the webinterface and pass the changed variables (e.g. a Twitter username) to the Python script so it can update its variables.
Unfortunatelly I have no idea how to pass data to the running Python script. Do you have any ideas?
-
1The easiest way would probably be to have it write to a common file that the python script will constantly poll and grab information from. Alternatively you could probably redirect stdin to that file. This is assuming that your script will be constantly running as you suggest. Probably best to do this with Threading. I'd start there.Bob– Bob2014年06月05日 19:03:31 +00:00Commented Jun 5, 2014 at 19:03
1 Answer 1
store modifiable settigns in a json file
settings.json
{"twitter_user": "bob"}
before doing something load your json settings
myscript.py
import json
def do_something():
settings = json.load(open("settings.json"))
print settings["twitter_user"]
update your settings.json via php as needed
myscript.php
function change_twitter_user($uname){
$settings = json_decode(file_get_contents($file));
$settings["twitter_user"] = $uname
file_put_contents("/path/to/settings.json",json_encode($settings ));
}
thats probably the easiest way to do it
(although you do know that python has some very nice web stuff also right?)