Sockjs and tornado: How Sent and Read Data - python

I’m trying to learn sockjs for a small university project.
I’ve a local server (with typical LAMP configuration), my goal is writing a python script (running on my local server) that is able to send data to client browser in order to update a text information.
Sockjs was seemed to me the best solution (tornado-sockjs for python server).
I read the tutorial examples (https://github.com/mrjoes/sockjs-tornado/tree/master/examples) but I’ve difficult to understand the basic functions.
It would be very useful to understand an example code. An example could be:
A python-server script generates a random number each 5 seconds and send this number to client. In webpage there is a definite element to update this value.
In the same webpage there is a button to send a text message to python server. When python server received this message, print it in the server console.
Thank you in advance for your help.
I hope that this post will be useful to other people.

Related

Transmit Pythonscript-Result to Webpage

Actual situation:
The client downloads a small pythonscript that is executable.
The client executes it. The script gathers information from the computer and sends the data to the webserver vià POST-Method.
Wanted Situation:
After the webserver recived the data, it should forward the information to the website-session of the client. And the website should display the information.
This is a visual example of the principle:
There is also a example of this principle on Can You Run It.
How can I realize this?
A common way of implementing this is using a RESTful API. Basically the API does not care if the request is from a script or web browser, it just passes data structures to and from clients. The only tricky part to your example is when there are multiple users involved because a secret must be shared between the browser and script. I believe Can You Run It puts this unique secret into the program they ask you to download.
Look into Django Rest Framework for examples of how to implement this.

Esp8266 Arducam- uploading to external IP

I have an Arducam OV2640
There is a lot of documentation on how the code works wth a HTML and js script which calls the camera IP address, after arducam creates a web server.
This web server listens to get requests and sends a post response of the image.
The only problem is this is all over local wifi...source:
https://github.com/ArduCAM/Arduino/tree/master/ArduCAM/examples/ESP8266/ArduCAM_ESP8266_OV2640_Capture
Has anyone done anything such as sending to Imgur via api or for instance a cloud based program like python anywhere?
I've been trying different methods all day, but a lot it comes to people saying "you need to send each byte and the other side have a handler."
I'm asking because the local wifi method works so easily I thought there must be a way to extend this to external addresses in a nicer way?
Has anyone done or heard of anything like this?
Thanks

Receive a web request with IFTTT

I am currently using IFTTT to create some automated software with Amazon Echo (Alexa). I want to use the IFTTT's Maker channel to do so.
Here's what I want the end result to be:
Command Amazon Echo (Alexa) to run a program.
Run a Python program on my computer.
I have had success in using the Trigger function of the Maker channel using a JSON request. However, there seems to be little documentation on the Action function, where IFTTT can make a web request to a URL. I have heard that webhooks may be needed to use this, again I am not sure where to get started with this.
The image below is what the Action function asks for. I know I'll need a server on my local machine or a program reading any requests sent to a public website.
If there are any libraries that would make this much easier, I would happily take recommendations, as this has been on my mind for a while..
Thank you!

Send data to App Engine

I have some code running on my Raspberry Pi (client running Python) that gets my public IP-address every hour. Now I want my IP to send that string to App Engine datastore.
Unfortunatly I do not know how to post data from a client, and all the examples I tried to google seems to handle browser inputs, not posting with Python from a client.
Does anyone know a good tutorial, or have some example code that a novice like me can understand? Maybe some pointers?
You just need to send your data as an HTTP POST. As Tim says in the comments, the third-party requests library is a great way to do this, although you can do it with the built-in urllib2.
From the server point of view, getting data from a POST like this is exactly the same as getting it from a form submission.

Python application communicating with a web server? Ideas?

I'm looking for a bit of web development advice. I'm fairly new to the area but I'm sure there are some gurus out there willing to part with some wisdom.
Objective: I'm interested in controlling a Python application on my computer from my personal web hosted site. I know, this question has been asked several times before but in each case the requirements were a bit different from my own. To reduce the length of this post I'll summarize my objective in a few bullet points:
Personal site is hosted by a web hosting company
Site uses HTML, PHP, MySQL, Python and JavaScript, the majority of everything is coded by me from the ground up
An application that is coded in Python will run on a PC within my home and will communicate with an Arduino board
The app will receive commands from the internet to control actuation via the Arduino, and will transmit sensor data back to the site (such as temperature)
Looking for the communication to be bi-directional, fast and secure
Securing the connection between site and Python app would be most ideal
I'm not looking to connect to the Python application directly, the web server must serve as the 'middle man'
So far I've considered HTTP Post and HTML forms, using sockets (Python app would run as a web server), an IRC bot and reading/writing to a text file stored on the web server.
I was also hoping to have a way to communicate with the Python app without needing to refresh the webpage, perhaps using AJAX or JavaScipt? Maybe with Flash?
Is there something I'm not considering? I feel like I'm missing something. Thanks in advance for the advice!
Just thinking out loud for how I would start out with this. First, regarding the website itself, you can just use what's easiest to you, or to the environment you're in. For example, a basic PHP page will do just fine, but if you can get a site running in Python as well, I'd prefer using the same language all over.
That said, I'm not sure why you would need to use a hosted website? Given that you're already forced to have a externally accessible PC at home for the communication, why not run a webserver on that directly (Apache, Nginx, or even something like CherryPy should do)? That webserver can then communicate with the python process that is running to control your Arduino (by using e.g. Python's xmlrpclib). If you would run things via the hosting company, you would still need some process that can handle external requests securely... something a webserver is quite good at. Just running it yourself gives you all the freedom you want, and simplifies things by lessening the number of components in your solution.
The updates on your site I'd keep quite basic: commands you want to run can be handled in the request handlers of the webserver by just calling the relevant (xmlrpclib) calls. Dynamically updating the page is best done by some AJAX calls I reckon. Based on your story, these updates are easily put in a JSON object, suitable for periodically updating only the relevant segments of your page.

Categories

Resources