Python Based Server

Wed, 03/06/2019 - 10:28

GITHUB Repository

In a previous article I noted how we were building simple interfaces to allow the production line and R&D to do certain tasks.

These tasks were affecting I/O to servos and switches etc, to automate or augment some tasks.

These tasks often relied on Python to do the I/O but we didn't want to use a built in interface like TKinter or WXPython. ... Not cool enough and a little slow.

So, we opted for a browser interface using LAMP. This was actually fine however it doesn't really make sense to have to use so many abstractions.

Most of the communication will be via JSON so the best solution was to simply use Python as a server. Seems simple enough.

And it was, ... with some caveats.

Due to cross-domain restrictions I couldn't simply use an HTML page to communicate with Python using AJAX. The browser didn't like that at all. So I built a basic structure to export various MIME types as necessary. Currently there is a route and a function for the base HTML, one Javascript and one CSS file.

Any further communication goes through an AJAX route that checks if the requested method exists, runs it and returns the response as JSON.

Python Based Server

Here's a basic rundown of the events. Note that the CSS and Javascript load via the AJAX portal by essentially the same method as any other request however, these special cases can be called from HTML through the /js and /css routes.

python server flow chart