Skip to content Skip to sidebar Skip to footer

Triggering Python Script Using AJAX Javascript Request On Local Server Using Vanilla JS

I have a localhost environment running using Python's SimpleHTTPServer, serving on port 8080. My project consists of an HTML file, a python script, and a Javascript (vanilla) scrip

Solution 1:

SimpleHTTPServer only serves file content. If you want to execute the script and return the output, you need CGIHTTPServer.

Note that both modules have been merged into http.server in python 3.


Solution 2:

You need to change the line: xhttp.open("GET", "./request.py", true); to xhttp.open("GET", "http://localhost:3333/<your-route>", true);. <your-route> is defined in your python file and I assume the python web server is running on port 3333.

If you are using Flask and define a route using for example: @app.route("/endpoint"), the request should look like:xhttp.open("GET", "http://localhost:3333/endpoint", true);


Solution 3:

You can use SocketServer instead of the default server. Add a handler to the SocketServer. Instead of a GET, do a POST with the actual command to run. The handler can check the post request and run the required script.


Post a Comment for "Triggering Python Script Using AJAX Javascript Request On Local Server Using Vanilla JS"