Python Forum

Full Version: Built a daemon, almost certinally the wrong way
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,
First time poster here, looks like a cool community.
My goal here was to build something that managed the lights on my Status Board Zero for my raspberry pi. Using the library provided (https://github.com/ThePiHut/statusboard) I quickly found out that once the script exited, the lights went out. The script I wrote creates a socket on the local loopback and listens for commands. I know that use of eval() on input is just a bad idea, but that's how I made it work. Looking for feedback, preferably constructive
https://github.com/Rhysers/statusboardd
myServer.py (I know I'm not creative when it comes to naming) is the listening script.

Thanks in advance!
Rhys
(Mar-17-2019, 09:11 PM)rhysers Wrote: [ -> ]I know that use of eval() on input is just a bad idea
In this case, because you're checking that the entire input string is within the valid list, I don't think it's that crazy here. There aren't any situations where you're running unknown code, as it needs an exact match.

That said, I think I'd still rather see a dict...
valid = {"internetGood": internetGood, 'internetOK': internetOK, 'internetBad': internetBad, 'alexaGood': alexaGood, 'alexaWarning': alexaWarning, 'alexaBad': alexaBad, 'stop': stop, 'noUpdates': noUpdates, 'secUpdates': secUpdates, 'updates': updates, 'needReboot': needReboot}

# ... and then...
if stringOut in valid:
    valid[stringOut]()
...if for no other reason than it allows you to change your architecture and rewrite either end of your code, without having to rewrite both the server and the client at the same time.