0

I’m currently building a website using flask. My flask page uses a downloadable package of executable files. I was wondering how I can code an html button to run a local exe file on button click.

Let’s say I have a file called test.exe. This is how I would imagine it: website.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route(“/home”)
def home():
    return render_template(“button.html”)

button.html

<button onclick=open/run test.exe>
</button>

What code would I put in place of “open/run test.exe”?

NewToPython
  • 97
  • 3
  • 9
  • 2
    If you mean after download that's not workable. Browser doesn't give access to what happens to actual files downloaded within window context or even any progress on a download – charlietfl May 31 '21 at 22:40
  • I thought you could make an HTML button execute an executable file from your system. Is that even possible? – NewToPython May 31 '21 at 22:48
  • 2
    Not directly , no. Think of what a security risk that would be if some random site was executing whatever on your device. Code you run in browser window is in a very isolated sandbox – charlietfl May 31 '21 at 22:49

2 Answers2

3

This will only work if the server is hosted on the computer, because otherwise it could be very unsecure. Otherwise you can not do this.

Python:

@app.route("/") # page with button
def my_page():
  return "<button onclick=\"var xhttp=new XMLHttpRequest;xhttp.open('GET','/openexe',!0),xhttp.send();\">run my file</button>"

@app.route("/openexe")
def open_EXE():
  os.system("path/to.exe")
  return "done"

This opens a request back to the server, and when the server gets this request the executable is opened.

UCYT5040
  • 367
  • 3
  • 15
0

You must write local service. But be carefull for security.

  • Web Site (HTML)
  • Local App Run Program ( exe file: listened on 8001 )

Algorithm

**Example HTML Call **

<button>Run Program</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
$("button").click(function(){
  $.get("//localhost:8001", function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});
</script>

Example App Run Program

# Python 3 server example
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import subprocess

hostName = "localhost"
serverPort = 8001

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        # IMPORTANT ENABLE HERE CORS 
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("<ok>Run Start Program</ok>", "utf-8"))
        # CHANGE HERE
        subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])


if __name__ == "__main__":        
    webServer = HTTPServer((hostName, serverPort), MyServer)
    print("Server started http://%s:%s" % (hostName, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.")
Hasan Delibaş
  • 470
  • 3
  • 14