skip to Main Content

I have an apache webserver installed on my ubuntu client. The webserver is being accessed by clients from LAN.
I want to have a HTML button, which starts a locally saved phyton script (/etc/skript.py) on my ubuntu client.

I’ve tried different things, but nothing seemed to work.
What’s the easiest way to do it?

Thanks for you help.

I tried this one out:

<button type="button" onclick="/etc/script.py">Click Me!</button>

2

Answers


  1. The value of an onclick attribute needs to be the source code for the body of a JavaScript function.

    Code running in web pages displayed in browsers have no way of executing files on the client.

    You can use a link or submit a form to a URL handled by the HTTP server and then make the HTTP server responsible for executing the script. This will run the script on the server, not the client (although they might be the same computer in your case… at least some of the time).

    Login or Signup to reply.
  2. As Quentin mentioned you can create a fastify server or express one, the below example is using fastify

    https://glitch.com/edit/#!/kindly-bald-gong

    Basically:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Execute Python Script</title>
      <link rel="stylesheet" href="/style.css" />
    </head>
    <body>
      <button onclick="executeScript()">Execute Python Script</button>
    
      <script>
        function executeScript() {
          fetch('/execute-script')
            .then(response => {
              if (response.ok) {
                console.log('Python script executed successfully.');
              } else {
                console.error('Failed to execute Python script.');
              }
            })
            .catch(error => {
              console.error('An error occurred while executing the Python script:', error);
            });
        }
      </script>
    </body>
    </html>
    

    fastify.get("/execute-script", function (request, reply) {
          exec("python ./src/scripts/skript.py", (error, stdout, stderr) => {
            if (error) {
              console.error("Failed to execute Python script:", error);
              reply.code(500).send("Failed to execute Python script");
            } else {
              console.log("Python script executed successfully");
              reply.send("Python script executed successfully");
            }
          });
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search