Execute Python script via JavaScript?
I have a home server in my home network and would like to start/execute a Python file via JavaScript (unless there is a better way) from the website I have set up there.
Does anyone know how this can be achieved?
(a) You can implement the server-side web application with Python. A microframework like Bottle, Flask or CherryPy is enough. You should either configure the web server accordingly (for Apache, for example, the mod_wsgi-Module) or you generally exchange the web server (for example for gunicorn).
(b) If you have already developed a server-side web application with a technology such as PHP, Node.js, or the like, the first option would of course be a little unfavorable, as you do not want to re-implement everything. As a rule, server-side web technologies have the possibility to send system commands to the OS via function and thus also to start Python scripts (if a Python interpreter is installed). In PHP, for example, the shell_exec-Function, Node.js has child_process– Module with the exec– Function.
(c) If you tend to call the Python script as described in the previous paragraph, you can also ask whether your Python script is really so Python-specific (for example, certain libraries that are only available in Python) or whether it could not be implemented with PHP/Node.js/… .
As regards the trigger that triggers the execution should be sufficient in the simplest case a GET request. You can use a simple link:
The target address refers to a request handler/script that executes the Python script/python code.
You can also fire GET requests with JavaScript. Either you let the target address load directly from the browser:
or you send an AJAX request:
Have now tried the following in my PHP file:
I want to achieve my discord bot, but nothing happens.
When I start the Python file, it does not work with the PHP file.
But if I use the path in the terminal, he does, he doesn’t know what the path should be “false”.
But if I use the path in the terminal, he does, he doesn’t know what the path should be “false”.
In the shell command, specify a correct path for the Python interpreter.
And how can I fix this problem?
I still assume that the Python interpreter cannot be found about your claim. If the command is executed, it will now be in the directories to which the environment variable LD_LIBRARY_PATH shows, unsuccessful after /bin/python3 searched.
Actually gives an error message:
“/bin/python3: symbol lookup error: /bin/python3: undefined symbol: XML_SetHashSalt ”
The Python interpreter is not in /usr/bin and python?
Otherwise, this return is not meaningful. Use exec or let you write the error message into the output channel by
to the shell command.
So when I get here
I’ll get NULL back.
For example
However, I get the desired result.
However, if I execute the commands in the terminal, both work.
What does the shell script return? There must be returns that have been executed by the system. Let’s do this var_dump output.
My PHP version is 8.2.0 if that helps.
What does the Python file mean to start directly? So why do you need JS or vice versa, why should the python part be in Python?
RegExp’s answer is quite detailed, but there are still things missing:
When it comes to performing the script and letting it display something, even as SSI, when interacting, the python script can be executed as CGI (et. al.).
For servers like Apache there are also mod_python.
Finally, you can perform other executeables in (almost) any language, so also the python script. If it is convenient, both components can also exchange/communate data via IPC.
So there are always a whole range of ways (Aja, Python can also set the whole HTTP daemon if you like it better, with framework will be more convenient).
It is possible, e.g. https://pyscript.net/
But it would be better to make it server-side
How this works there are enough tutorials on Youtube
Didn’t want to find what I should look for if I want to make it server-side.
Search for “python server website”
Python can also be used instead of PHP or NodeJS but is not so widespread