0

I am on a dreamhost server and have some HTML that calls some javascript when a button is pressed. I am trying to call a python script when this button is tapped.

First off, to my knowledge as I am on a shared host I cannot use AJAX as it is not supported, so I need to do this without AJAX. Right now I am trying to do a XMLHttpRequest which is working.

I also realize doing an XMLHttpRequest is not the best way since the files are both on the server there must be a way to just call the file directly?

So if someone call tell me how to call it directly or help me fix this error in the browser console that would be great. Thanks for the help

EDIT

I have an HTML file when a user taps a button on this file it calls some javascript that is in the HTML file. This javascript currently makes a POST Request, to a python script that is on the same server and the HTML file.

What I want instead of making a post request to the python file that is on the server, I want to just directly call the python file, from the javascript that runs, when the button the clicked in the HTML file.

Both the HTML file which contains the javascript and the python file are on the same server. And I don't want the python to run in the browser, I want it to run in the background on the server.

How can I use the Javascript to call this python file?

spen123
  • 3,464
  • 11
  • 39
  • 52
  • Are you using any sort of middleware to do the server side routing? Are you using nodejs/express, python/(cherrypy, flask), or something else like nginx/apache? The only way that I know of to run "python" in the browser is to use a separate library that transpiles python into javascript. – jmunsch Nov 28 '15 at 06:27
  • @jm_____ I don't want the python to run in the browser I just want to to be called on the server, do you know what Im getting at. Right now I just do it though a POST request but the files are on the same server, so I want to do with without the post, but I don't need it to run in the browser – spen123 Nov 28 '15 at 06:29
  • Which files are on the same server? can you include an example in your question, and include how you have the files structured? – jmunsch Nov 28 '15 at 06:31
  • @jm_____ i just explained in the edit – spen123 Nov 28 '15 at 06:35
  • http://stackoverflow.com/questions/89228/calling-an-external-command-in-python - you will need to ajax the call to the server – mplungjan Nov 28 '15 at 06:54
  • @mplungjan Im not sure how this helps I want the javascript to call the python – spen123 Nov 28 '15 at 06:55
  • JS in a browser cannot call a python on the server on its own. It needs to make an http request to the code. If it does not return something that is fine, but the trigger is the http request. If you want js on the server to call python on the server you need something like node.js – mplungjan Nov 28 '15 at 06:57

1 Answers1

3

As far as I understand your question what you are looking to do is called a "remote procedure call", or some sort of Service Oriented Architecture (SOA).

You are on a right track in making a POST request to the server.

You can setup a middleware like flask, or cherrypy to run the script when you send a GET, PUT, POST ... request. And inside of the middleware controller you can call your script.

Basically you have started to create a RESTful api, and its a pretty standard way these days to run logic on the backend.

Some examples of different frameworks for doing url routing:

Python:

NodeJs:

Also very good is this question: JSON, REST, SOAP, WSDL, and SOA: How do they all link together

Another way that you could do this from the browser would be to use sockets, which opens a connection between the client and the server.

Inside the javscript you could use socketio:

<script src='/socket.io/socket.io.js'></script>
<script>
    var socket = io();
    socket.connect('http://localhost:8000')
    socket.emit('run_a_script_event', {arg1: 'hello', arg2: 'world'});
</script>

And in your python code you could use the socketio client for python (https://pypi.python.org/pypi/socketIO-client):

from your_module import thescript
from socketIO_client import SocketIO, LoggingNamespace

def run_a_script(*args):
    print('arg1 and arg2', args)
    thescript()

socketIO = SocketIO('localhost', 8000, LoggingNamespace)
socketIO.on('run_a_script_event', run_a_script)

Looks like there is also a version specifically for flask: https://flask-socketio.readthedocs.org/en/latest/

Or you could run the python directly in the browser by converting it to javascript with a transpiler:

Or you could use node javascript to spawn a child process which runs the python script:

Or you can import the python script into node like:

Community
  • 1
  • 1
jmunsch
  • 22,771
  • 11
  • 93
  • 114