Send json from Python script to Node-red locally - python

I currently have a Python script sending data over to an IoT Hub and a node in a Node-red flow receiving that information, but for some cases that would not work (ex. when internet is down).
I'm wondering if there is any way I can adapt my Python script to get that json object sent directly to Node-red bypassing any communication over the Internet.
Any hint would be appreciated!

You could add in a messaging solution in the middle. You would use something like a Python MQi library, which would be able to provide you with assured message delivery at the Python end when the network connection re-establishes. There are a varied set of MQ and MQRest Node-RED nodes that you can use on the Node-RED end listening for messages.

Related

WebRTC with python

I would like to make a streaming server with python/twisted, which receives a WebRTC video stream and then applys some OpenCV algorithms to it.
However I cannot find a python module for WebRTC. How can I send and receive a WebRTC video stream with python/twisted?
Thanks!
I have started putting together the basic blocks needed to create a Python WebRTC endpoint.
One is an asyncio-based Interactive Connectivity Establishment module:
https://github.com/jlaine/aioice
Another one is a Python binding for libsrtp:
https://github.com/jlaine/pylibsrtp
We also need SRTP keying support in the OpenSSL bindings:
https://github.com/pyca/cryptography/pull/4099
On top of this, we can then build an asyncio-based WebRTC implementation:
https://github.com/jlaine/aiortc
I have been able to get both Chrome and Firefox to establish an audio and video stream to a Python-based server.
What you can do is take screen shots continuously and push them to a websocket and allow your twisted server to take a gander at each one as it comes in.
I have modified some common recorders and my version takes Jpeg images and pushes them over a websocket. Feel free to use and modify how you want so that it fits your needs. Source code here. The example I use is pushing down to a libwebsocket server built in C but the same javascript could be used to send to any websocket server.
I've had a similar issue and ended up creating a server that launches a headless chrome instance from which I can access the WebRTC streams, record chunks with a MediaRecorder and finally forward those chunks on via a WebSocket.
I'd love a python based solution so I wouldn't need the intermediary server launching headless chrome instances but haven't been able to find one.
I've been using Node.js and Puppeteer but one could launch the browser instances from your python server and then send the decoded data back via plain old sockets or whatever else tickles your fancy.

How to communicate between client device and webserver?

I've build a little device based on the raspberry pi. Now I want to configure it using my web server. The idea is that I enter all the details on my django web page and then the device just pulls that off the server.
But there are two problems I'm not sure how to solve:
I have multiple devices for multiple users so some kind of Login must be provided.
The device also sends pictures from time to time. Right now it's using FTP with a general login, but I want to personalize that too for every device. The uploads will need a resume function so http is out!
So the basic question is: Should I get started with sockets or is there a better and safer way to do it? Maybe there is some kind of open source library that's been tested a lot?
Instead of hand coding sockets, I would suggest using HTTP with BASIC authentication to communicate between the device and the web server. You can uniquely assign an id/pwd to each device, and BASIC authentication is well supported by all web servers and client side libraries.
There are some security concerns with BASIC authentication even if you use HTTPS, but it maybe acceptable in your particular case here.
Maybe you could use SSH, with Fabric for instance. Here an example.

AutobahnPython Server HTML5 Front end

I have this AutobahnPython server up and running fine.
https://github.com/tavendo/AutobahnPython/blob/master/examples/websocket/streaming/streaming_server.py
I want to attach a HTML5 Front end for capture of web cam video and audio.
How do I get the HTML5 Blob to send through the socket I just created in HTML5 to the python socket server I also have running?
Is it sendMessage?
https://autobahnpython.readthedocs.org/en/latest/websocketbase.html#autobahn.websocket.WebSocketProtocol.sendMessage
Be prepared, doing what you want, and doing it right (which means flow-control), is an advanced topic. I try to give you a couple of hints. You might be also interested in reading this.
WebSocketProtocol.sendMessage is part of the AutobahnPython API. To be precise, it is part of the message-based basic API. Whereas the streaming server above uses the advanced API for receiving, it uses the basic API for sending (since the sent data is small, and there is no need for flow control)
Now, in your case, the web cam is the "mass data" producer. You will want to flow-control the sending from the JS to the server. Since if you just send out WebSocket messages from JS as fast as you get data from cam, your upstream connection might not keep up, and the browser's memory will just run away. Read about bufferedAmount which is part of the JS WebSocket API.
If you just want to consume data is it flow into your server, above AutobahnPython streaming server example is a good starting point since: you can process WebSocket data as it comes in. Other WebSocket frameworks will first buffer up a complete message until they give the message to you.
If you want to redistribute the data received by your server again to other connected client, you will want flow-control on the server's outgoing leg also. And then you will need the advanced API for sending also. See the reference or the streaming (producer) client examples - you can adjust the code to run inside your server.
Now if above all does not make sense to you .. it's a non-trivial thing. Try reading the first link to the Autobahn forum, and more about flow-control. It is also non-trivial since the JS WebSocket API has only limited machinery for doing this kind of flow-control, without falling back to invent your own scheme at app level. Well. Anyway, hope that helps a little.

Python JSON-RPC_2.0 TCP Server Client Explained

I'm having a difficult time fully understanding the nature of a TCP server/client relationship when a JSON string is sent to the server. The information I need may be out there, but I'm perhpas not using the correct search paramaters as I'm looking.
I've built a Python TCP, JSON-RPC Server from the following examples:
https://github.com/joshmarshall/jsonrpclib
http://code.activestate.com/recipes/552751-json-rpc-server-and-client/
In both cases, I can communicate with the Python server from a Python console on a different computer, sending commands from one (the client) to the other (server). In all of the examples, I've had to install the libraries mentioned above on both the client and the server machines in order to facilitate the TCP communication.
So the background to my situation and question is, when does JSON enter the mix? This is what I want to do:
Setup a Python TCP server that accepts a JSON string from a remote client inside (or outside) the network. The server parses the JSON string, fetches the method and parameters from the objectified string, and executes the method. The server then sends a JSON string result to the calling client. In this case, the client is a mobile application (iPad, Android, etc) with a JavaScript library that I'll use to send the requests to the server.
Why would I need a Python client? From what I can gather, the client just needs to open a connection to the server and then send the JSON string, right? Why do all the code samples include Python client examples? Are they assuming a server machine is going to talk to a server machine, so they have included client code to help generate the JSON string that will be sent to the server?
If I assume that a Python client isn't really needed for anything, I've been sending JSON strings to the Python server from the iPad, but in each case the server is reporting a "Bad request syntax" error. I'll pen a new question on that issue if I'm understanding the current question correctly.
Insight is appreciated.
The JSON encoding is the lingua franca of your RPC protocol, so you can indeed use any client you like. The implementations you found for JSON-RPC use the HTTP protocol, a very specific communication protocol built on top of TCP/IP, but you can implement the same protocol over raw TCP-IP sockets if so required.
The examples include both the Python client and the server because they illustrate how to implement the JSON-RPC standard in Python, not in JavaScript or C or Lisp. They focus on the implementation in one language. The JSON-RPC standard however, is language agnostic. It doesn't matter what language you write either the server or the client in, as long as they use the same standard.

How do I go about writing a program to send and receive sms using python?

I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!
Are there GSM libraries for python out there?
Have you looked at py-sms?
Python-binding for Gammu perhaps?
BTW. It's GUI version (Wammu) is written in wxPython.
I have recently written some code for interacting with Huawei 3G USB modems in python.
My initial prototype used pyserial directly, and then my production code used Twisted's Serial support so I can access the modem asynchronously.
I found that by accessing the modem programatically using the serial port I was able to access all the functionality required to send and receive SMS messages using Hayes AT commands and extensions to the AT command set.
This is the one of the referneces I was able to find on the topic, it lists these commands:
AT+CMGL List Messages
AT+CMGR Read message
AT+CMGS Send message
AT+CMGW Write message to memory
They are complicated commands and take arguments and you have to parse the results yourself.
There are more references on the internet you can google for that reference these 4 commands that will let you work out how your modem works.
I provided a detailed answer to a similar question here. I mention a Python interface to an SMS gateway provided by TextMagic.
Also check PyKannel.
There's also a web service for sending and receiving SMS (non-free, of course), but I don't have URL at hand. :(
I was at a hackday where we had free access to a few APIs, including twillio. It was very easy to use their python library to send and receive SMS, and we built an app around it in about 3 hours (twillio integration was about 15 minutes).

Categories