Execute Python script from web via Domoticz internal server

Python and python framework

Moderator: leecollings

Post Reply
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Execute Python script from web via Domoticz internal server

Post by EdddieN »

Hello,

I'm trying this with Python, to create a setup page for this project:
http://www.domoticz.com/forum/viewtopic ... 40#p178879 and the challenge I'm having is that Domoticz internal webserver does not execute Python scripts outside its own management system.

I guess I could solve this using Apache or Nginx, but seems a bit overkill to install another server when Domoticz has already one. This is the architecture:

1.- I have a web setup page with multiple options for its nearly 50 I/Os, some of them have multiple options per I/O, hence the reason to create its own custom setup page
2.- From this page, it generates a configuration file that is pushed via UART to the guest micro-controllers on the main board.
3.- Similar for reading the status settings of the main board.

I also tried with PHP, same situation. The question, can I call from a web page within the www folder of Domoticz a python script and execute it?
11101101 - www.machinon.com
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by zak45 »

EdddieN wrote: Monday 30 April 2018 13:27 the challenge I'm having is that Domoticz internal webserver does not execute Python scripts outside its own management system.
The question, can I call from a web page within the www folder of Domoticz a python script and execute it?
Hi, have a python plugin for that that I'm calling PSExec ( if you see the reference ;-). Able to execute any kind of command on system where Domoticz is installed, so if you want to execute a python script, this can be done without problem. I'm using it actually to execute some specific command for my voice system control.
Let me know if you are interested, can provide it to you.
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by EdddieN »

Yes interested! Are you able to trigger it from a web page?
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by EdddieN »

Ok, I saw your plugin, really nice! Just to advance for me, I get lost :D

To give you a more real example of what I'm tying, I have a simple website served by Domoticz webserver, this webpage produces a configuration file or similar with the above:

Code: Select all

4;1;1;0;24;1\n
4;1;1;0;25;99,345\n
4;1;1;0;26;30,38\n
4;2;1;0;24;1\n
4;2;1;0;25;99,345\n
4;2;1;0;26;30,38\n
4;3;1;0;24;1\n
4;3;1;0;25;99,345\n
4;3;1;0;26;30,38\n
4;4;1;0;24;1\n
4;4;1;0;25;99,345\n
4;4;1;0;26;30,38\n
4;5;1;0;24;2\n
4;5;1;0;25;99,345\n
4;5;1;0;26;30,39\n
4;6;1;0;24;2\n
4;6;1;0;25;99,345\n
4;6;1;0;26;30,39\n
4;7;1;0;24;2\n
4;7;1;0;25;99,345\n
4;7;1;0;26;30,39\n
4;8;1;0;24;2\n
4;8;1;0;25;99,345\n
4;8;1;0;26;30,39\n
Which I need to send via serial port. I have a python/bash script that already pushes the data to the motherboard, however I cannot execute the script from the web itself... any ideas how to bypass this without having to install a new server?
11101101 - www.machinon.com
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by zak45 »

EdddieN wrote: Tuesday 01 May 2018 11:48 Ok, I saw your plugin, really nice! Just to advance for me, I get lost :D

Which I need to send via serial port. I have a python/bash script that already pushes the data to the motherboard, however I cannot execute the script from the web itself... any ideas how to bypass this without having to install a new server?
Hummm.. I think I see the one you're speaking.. not mine and I'm also lost :-)
Mine request that you send the command you want by doing a POST on specific port, so this can be made by curl and of course any web page.
curl example :
curl --data "ls -altr >$HOME/ls.lst" 192.168.1.20:9002
PSexec.zip
(7.9 KiB) Downloaded 28 times
Do not be afraid about the code inside, do not have take time to clear it and made something better.. but work as it is.
you just need to change this line and put the command you want to allow:

Code: Select all

#
# Authorized commands, if not into the list, not executed
commandok=['samsungctl','dir','py', 'tt.cmd']
you need to adapt also the psexec.sh, something like that:
nohup sudo /bin/sh $1
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by EdddieN »

Thanks!

I'll have to play with it a bit, I'll come back with feedback hopefully in a few days :)
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Execute Python script from web via Domoticz internal server

Post by EdddieN »

Just thinking aloud, what about forgetting about the whole web page served by Domoticz but actually building a plugin:
https://www.domoticz.com/wiki/Developin ... hon_plugin

From what I can see here http://www.domoticz.com/forum/viewtopic ... 65&t=21297 , python can access the UART of the Raspberry Pi (or any UART I guess), so I could create the setup page as a plugin, right?

If anyone has a simple way to explain how it works, I'll take it! as I'm really new to it, it is going to take me a while :lol:
11101101 - www.machinon.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest