Execute .py script from the local folder (www)

Does anybody know how to execute a script (.py) from the local folder (www)?
Script test.py is saved in: /home/homeassistant/.homeassistant/www
URL: http://MY_HA_SERVER:8123/local/test.py

It is working fine for html pages but I would like to execute a .py file? Is it possible?

I don’t know if I really understood what you want to do, but I think what you are looking for is a way to run a python script from a php file via the browser.
This definitely requires some privilege settings. Apache web server runs any script as user www-data and thus www-data needs to have access to your test.py .
As your python file is outside of the web servers directory (which is a good thing, in fact) www-data would need to be able to run the script as sudo. So you need to edit the /etc/sudoers file.
Change to /etc and run sudo visudo(DO NOT attempt to edit the file otherwise, typing errors might cause trouble and sudo visudowill check the syntax before saving.

You might edit the sudoers file as follows, i.e. adding those lines:

%www-data ALL=NOPASSWD: /usr/bin/python3 /home/homeassistant/.homeassistant/www .../test.py

Please adapt your path to the python you are using.

In your php file you can run your test.py like this:

exec("sudo python3 /home/homeassistant/.homeassistant/www .../test.py");


Is PHP installed by default in the HA web server? I know how to add PHP to an apache server but I have no clue with HA web server…
Any idea?

OK, I think I got what you wanted. Sorry, but I don’t know and have never tried in fact.

I know how to do it with PHP but I have no idea what is the web server used in Home Assistant.
What is the web server behind HA? Does it accept cgi-bin or something similar to execute Python scripts?