2

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?

1
  • 1
    The 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. Commented Jun 5, 2014 at 19:03

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I also thought about outsourcing the settings into a file, but I thought it is verry hacky to access a file everytime before I fetch the new Tweets... I never used webstuff in Python (acually I like Twig and bootstrap in PHP) are there some nice examples/tutorials?
naw just check it when your idle .... if they changed it a fraction of a second before they hit the button to get tweets or whatever who cares ... just watch the file timestamp in your onIdle loop or whatever ... another option would be running a socket server in python and sending socket messages from php ... but you are adding alot of unnecessary complications
OK I think I will stick to the file method. It's easy and doeas what I need. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.