Get nodejs server to trigger a python script - python

I have a node.js server running on a Raspberry Pi 3 B+. (I'm using node because I need the capabilities of a bluetooth library that works well).
Once the node server picks up a message from a bluetooth device, I want it to fire off an event/command/call to a different python script running on the same device.
What is the best way to do this? I've looked into spawning child processes and running the script in them, but that seems messy... Additionally, should I set up a socket between them and stream data through it? I imagine this is done often, what is the consensus solution?

Running a child process is how you would run a python script. That's how you do it from nodejs or any other program (besides a python program).
There are dozens of options for communicating between the python script and the nodejs program. The simplest would be stdin/stdout which are automatically set up for you when you create the child process, but you could also give the nodejs app a local http server that the python script could communicate with or vice versa.
Or, set up a regular socket between the two.
If, as you now indicate in a comment, your python script is already running, then you may want to use a local http server in the nodejs app and the python script can just send an http request to that local http server whenever it has some data it wants to pass to the nodejs app. Or, if you primarily want data to flow the opposite direction, you can put the http server in the python app and have the nodejs server send data to the python app.
If you want good bidirectional capabilities, then you could also set up a socket.io connection between the two and then you can easily send messages either way at any time.

Related

Approach to multi-threaded, multi-processing, async python embedded in c++ with PyBind

I wanted to do a stack inquiry before I do a deep dive tomorrow and start trying out every option under the sun. We have an application that we've embedded python inside with PyBind 11. In the current implementation application events are triggering an xml-rpc call to a remote server and its working fine.
We are looking, however, to extend this functionality by either adding a websocket server, connecting into Kafka or MQ or some other sort of functionality - allowing for both incoming and outgoing messages.
Where I'm running into some design questions is as I understand I have 1 interpreter which means I likely (by default) have 1 thread available.
I'm trying to figure out what approach gives me the following:
Application events in C++ will be allowed to call into the Python interpreter and fire off events
Events received by the python interpreter will call back into C++
I'm pretty sure this is easy to setup one way or the other but I'm getting confused how to handle both things at once.
I can setup a websocket server easily enough - but if my server is actively running
start_server = websockets.serve(counter, "localhost", 6789)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Am I still able going to be able to make additional calls into the interpreter or is Python going to be locked up?
In summary:
When using embedded Python - if I have an actively running process (say listening for messages on a websocket) am I still able to make calls into the Python interpreter to do additional things - or am I blocked?

Using pjsip in daemon mode with python twisted

I am trying to run simple pjsip application in daemon mode. I have combined this library with python twisted. Script works fine when I run it in shell & can make call. But when I use it with twisted's Application framework, I get following error.
Object: {Account <sip:192.168.0.200:5060>}, operation=make_call(), error=Unknown error from audio driver (PJMEDIA_EAUD_SYSERR)
Most of example applications from documents do not run in daemon mode - pjsip examples.
Looks like even pjsua doesn't run in background - pjsua
I am wondering, does it work in background. I am not getting exactly what "Unknown error" meant to. Is there any better way to debug ?
Architecture of my application is as follows -
Start pjsip lib, initiate pjsip lib, create transport & create userless account.
Create UDP protocol which listens for incoming requests.
Once app gets request, it makes calls to particular sip uri.
Everything goes well when I run app with listenUDP & reactor.run() but when I tries with typical twisted application setup - twistd( either listenUPD or UDPServer) above error pops up.
Am I doing anything wrong ? Any info will be welcomed.
thank you.
This issue resolved after I set sound devices.

Raspberry Pi python app and nodejs socketio communication

My requirement is to communicate socketio with nodejs server to Raspberry Pi running a local Python app. Please help me. I can find ways of communication with web app on google but is there any way to communicate with Python local app with above mentioned requirements.
It's unclear exactly which part you need help with. To make a socket.io connection work, you do the following:
Run a socket.io server on one of your two computers. Make sure it is listening on a known port (it can share a port with a web server if desired).
On the other computer, get a socket.io client library and use that to make a socket.io connection to the other computer.
Register message handlers on both computers for whatever custom messages you intend to send each way and write the code to process those incoming messages.
Write the code to send messages to the other computer at the appropriate time.
Socket.io client and server libraries exist for both node.js and python so you can either type of library for either type of system.
The important things to understand are that you must have a socket.io server up and running. The other endpoint then must connect to that server. Once the connection is up and running, you can then send message from either end to the other end.
For example, you could set up a socket.io server on node.js. Then, use a socket.io client library for python to make a socket.io connection to the node.js server. Then, once the connection is up and running, you are free to send messages from either end to the other and, if you have, message handlers listening for those specific messages, they will be received by the other end.

sending data from one thread on host machine to some kind of service thread running on the other machine over network

I want to send some commands from my host machine to the other machine over the network.
What I want is some kind of listener or service thread which is running all the time on the other machine and soon it encounters a command from the host act accordingly.
How can I achieve this in python?
I think you are confusing IPC (interprocess communication) with RPC (remote procedure call). IPC is something that happens exclusively locally while RPC is something that you can use to execute stuff on a remote system.
What you want to do here is basic client-server communication scenario so I suggest you look at the thousands of tutorials on the Internet how to achieve that. A properly implemented server has one or multiple service listeners so the scenario you are referring to is a pretty standard one.

How to start a privileged process through web on the server?

I have created a web-app using Python Flask framework on Raspberry Pi running Raspbian. I want to control the hardware and trigger some sudo tasks on the Pi through web.
The Flask based server runs in non-sudo mode listening to port 8080. When a web client sends request through HTTP, I want to start a subprocess with sudo privileges. (for ex. trigger changes on gpio pins, turn on camera etc.). What is the best practice for implementing this kind of behavior?
The webserver can ask for sudo password to the client, which can be used to raise the privileges. I want some pointers on how to achieve this.
Best practice is to never do this kind of thing. If you are giving sudo access to your pi from internet and then executing user input you are giving everyone in the internet the possibility of executing arbitrary commands in your system. I understand that this is probably your pet project, but still imagine someone getting access to your computer and turning camera when you don't really expect it.
Create a separate process that does the actual work for you. You can run this process in the background with elevated privileges.
Your web client will just deposit tasks for this process in a queue. This can be as simple as a database to which your Flask app writes a "task request", the other, privileged process reads this request, performs the action, and updates the databases.
This concept isn't new, so there are multiple brokers and task queues that you can use. Celery is popular with Python developers and is easily integrated into your application.

Categories