Javascript library for displaying Python console app output - python

I'm writing a simple web-based front-end for a Python console program that runs on a local machine. The interactions are extremely simple. Essentially, the web front-end needs to:
Accept input from the user (through an AJAX form or something).
Pass this input to the Python program and run it.
Display the output of the Python console program while it is running, until it terminates.
The first two can be accomplished quite easily (though suitable AJAX library recommendations would be helpful).
Question: What Javascript library would I need to accomplish No. 3?
Remarks:
I am aware of packages like AJAXterm and Shellinabox, but instead of a full-shell, I just want to display the output of the Python console program; essentially I'd like a way to pipe Python's stdout to a web-page in real-time.

You are probably looking for a comet implementation or another server push protocol, because of the unpredictable timing of python code output. So on the server side you have a thread that is reading from your python process' stdout and pushing out the output to your client via comet.
cometd may be your best bet for the client & server components.

Related

Paramiko: "Must be connected to a terminal."

I am trying to use Paramiko to access the input and output of a program running inside of a screen session. Let's assume there is a screen screenName with a single window running the program I wish to access the I/O of. When I try something like client.exec_command('screen -r screenName') for example, I get the message "Must be connected to a terminal" in the stdout.
Searching around, I learned that for some programs, one needs to request a "pseudo-terminal" by adding the get_pty=True parameter to exec_command. When I try this, my code just hangs, and my terminal becomes unresponsive.
What else can I try to make this work?
To provide a little more context to my problem, I am creating a web app that allows users to perform basic operations that I have previously done only in a PuTTy terminal. A feature I wish to have is a kind of web terminal designed to interact with the input and output of this particular program. Effectively all I want to do is continuously pipe output from the program to my backend, and pipe input to the program.

How to send python list to C++ application awaiting input?

I am writing a python script that generates a list of integers and is supposed to send this list to a concurrently running C++ program that is constantly awaiting input. What is a quick and painless way to design the link between these two languages?
I have considered possibilities such as:
writing the python result to a file, while listening for file updates in C++ and loading it as soon as new data is available.
using python pexpect package and including a command line interface in the C++ program, so that it receives the data through cin.
Both above possibilities seem a bit too hacky to me. Are there better options, which would not require to become an expert in C++ library coding for python?
You could set up a Socket and an OutputStream in the python app to send the data, and another Socket and an InputStream in the C++ program to receive the data.

Passing arguments to a running python script

I have a script running on my raspberry, these script is started from a command from an php page. I’ve multiple if stetements, now I would like to pass new arguments to the script whithout stopping it. I found lots of information by passing arguments to the python script, but not if its possible while the svpcript is already running to pass new arguments. Thanks in advance!
The best option for me is to use a configuration file input for your script.
Some simple yaml will do. Then in a separate thread you must observe the hash of the file, if it gets changed that
means somebody has updated your file and you must re/adjust your inputs.
Basically you have that constant observer running all the time.
You need some sort of IPC mechanism really. As you are executing/updating the script from a PHP application, I'd suggest you'll look into something like ZeroMQ which supports both Python and PHP, and will allow you to do a quick and dirty Pub/Sub implementation.
The basic idea is, treat your python script as a subscriber to messages coming from the PHP application which publishes them as and when needed. To achieve this, you'll want to start your python "script" once and leave it running in the background, listening for messages on ZeroMQ. Something like this should get you going
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
# Wait for next message from from your PHP application
message = socket.recv()
print("Recieved a message: %s" % message)
# Here you should do the work you need to do in your script
# Once you are done, tell the PHP application you are done
socket.send(b"Done and dusted")
Then, in your PHP application, you can use something like the following to send a message to your Python service
$context = new ZMQContext();
// Socket to talk to server
$requester = new ZMQSocket($context, ZMQ::SOCKET_REQ);
$requester->connect("tcp://localhost:5555");
$requester->send("ALL THE PARAMS TO SEND YOU YOUR PYTHON SCRIPT");
$reply = $requester->recv();
Note, I found the above examples using a quick google search (and amended slightly for educational purposes), but they aren't tested, and purely meant to get you started. For more information, visit ZeroMQ and php-zmq
Have fun.

dump1090 offline raspberry pi

I'm currently doing a project in which I'm making an ADS-B flightradar on a led matrix, which is controlled by a Raspberry Pi. I've found a program called dump1090 which receives and decodes the data from my SDR receiver. I can find lots of example on how to use to forward that data to a webserver or whatever, but I can't seem to find anything on how you can programmatically listen to the data dump1090 produces. Does anyone know how you can programmatically receive dump1090's data in order to use the data in a program? (any language would do, but perhaps python would be the most obvious choice)
You should be able to start dump1090 using a programming language of choice (c/c++/java/python/etc.) and and read the std out pipe.
Personally, on Raspberry Pi, I find Python nicer to use since it's easier to test/reiterate without needing to compile. Python provides the subprocess package which allows you run dump1090(or any other application) from within Python and have a look at the output (using subprocess.check_output('dump1090') for example). Have a look at check_output and Popen options to see what works best with your application.

Calling a python program from an external link and communicating with it

I have no idea if this has an official name, nor do I know a better way to really describe what I'm going for here so I'll list the two things I'd like to accomplish.
In a nutshell I have a task management program that emails people when they've been assigned a task. What I'd like is some way to generate a link (html or otherwise) that can be clicked that will either:
Launch the program with some command-line arguments, or
If the program is already open, communicate some commands to it, so that it could be, perhaps, set as the top window and then it could switch to the task.
I know this must be possible, but I have not yet gleaned the knowledge of how to communicate with programs outside of themselves. Any ideas? Thanks in advance!
I use tools like itty and flask to implement this kind of functionality.
For example, using itty here's how you could launch a program with some command-line arguments:
from itty import *
#post('/someprogram/(?P<args>\w+)')
def launcher(request, args):
'Launch a program with some command-line arguments'
return subprocess.check_output('someprogram', shlex.split(args))
run_itty()
The front-end for this could be a simple web page with an html form to get command-line arguments and a submit button to make the POST request.
This approach works equally well over a local network or over the web.

Categories