Python + URL Params
Posted: Fri Mar 17, 2023 9:45 am
So I have finally got a web interface setup, and I can visit example.com/cgi-bin/on.py and it will turn my socket on, I don't know much python though, so I was wondering if it would be possible to control the power/device via the URL?
This is the code I am currently using:
I assume I would need to create one Python script to control the power (On and Off, for other devices too), and create some variables. How would I do this for on/off?
My goal, would be to have it so I can type in example.com/cgi-bin/power.py?id=1;power=0
Or something similar, which obviously would turn off the device that has the ID '1'.
Is this possible? I'm sure it shouldn't be too difficult, from what knowledge I have from other programming languages, I'm just not too sure how to do it in Python, and I couldn't really understand what I found on Google.
This is the code I am currently using:
Code: Select all
print "Content-Type: text/html"
print
print '<title>Power - ON</title>'
print "<h1>Fan - <font color=green>On</font></h1>"
print 'Please click <a href="/index.html">here</a> to go back.'
from tellcore.telldus import TelldusCore
core = TelldusCore()
devices = core.devices()
for device in devices:
if device.name == "Switch":
device.turn_on()
My goal, would be to have it so I can type in example.com/cgi-bin/power.py?id=1;power=0
Or something similar, which obviously would turn off the device that has the ID '1'.
Is this possible? I'm sure it shouldn't be too difficult, from what knowledge I have from other programming languages, I'm just not too sure how to do it in Python, and I couldn't really understand what I found on Google.