1

I have an (html/js) application running on my localhost. I'd like to send some information (string) to a python script running in the background, which will just print the string, or save it as a .txt-file.

It seems websockets will do the job, but I cannot get around this (in my eyes a simple problem..?). All examples or libraries aims at specific usages or are depricated in the meanwhile. Also, maybe someone can point me to another principle like REST.? I'm not really into web/ip/internetthings, but I need this to let the webpage initiate some python programs. Any tips on how to achieve this?

1 Answer 1

1

I am doing something similar to have a web server (nodejs) control my raspberrypi (python). I suggest you simply spawn your python script by your js server and make them communicate via stdin/stdout.

For example with nodejs:

var spawn = require('child_process').spawn;
var child = spawn(
    'python3',
    ['./py/pi-ctrl.py']
);
var child_emit = function (message) {
    child.stdin.write(message+"\n");
}

Then your js can just 'emit' anything to your python script, which listens to stdin:

while True:
    line = input().split()
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, I don;'t have a node server, but for now I'm running everything from my localhost. Later on, the package could be placed on a python server.
What do you mean then by 'localhost'? I understand your html/js is not served yet by a server, you just open them in a browser, right? If you have no server, I see no way you can have messages exchanged between your python script and your web app. Maybe try django?
I temporarily use Tomcat or the builtin server by PyCharm to host the files on my localhost. Once done, I can access the iapplication trhoguh localhost:8080 or localhost:63343. Probably your question also perfectly illustrates my misunderstandings of 'the internet'...
Don't worry. So basically you have tomcat serving your html/js/css files on port 8080 (HTTP), and a standalone python script that should write into files. So yes I advise you start using django and include your python script into your server code. Then you can choose between HTTP requests (REST) or websockets, whatever looks simpler to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.