Georg
Georg

Reputation: 219

Send command from PHP to running Python script

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?

Upvotes: 2

Views: 985

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114038

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?)

Upvotes: 2

Related Questions