From charlesreid1

Revision as of 18:36, 25 February 2017 by Admin (talk | contribs) (→‎Flask)

Running Web Server on Raspberry Pi

If you're running a web server on Raspberry Pi, you'll probably want to run on port 80. However, Raspberry Pis sometimes have a problem when installing Python packages into /usr/local/lib, so you have to install them into a custom directory. This creates a problem when trying to import the package as root, because root python won't be able to find it (won't have the $PYTHONPATH variable set correctly).

To get around this, you can bind to port 80 without being root by following these instructions:

http://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-processes-to-bind-to-privileged-ports-1024-on-l

http://www.wensley.org.uk/info#setpcaps

Flask

Flask is an excellent lightweight web server in Python. Flask makes it easy to create an API for requesting data and passing JSON between Python (which is a powerful language that can control all kinds of things on the Raspberry Pi) and Javascript (which provides a nice user front-end via a web interface).

For an example of how you might do this, see my Dymouse project on Github: http://github.com/charlesreid1/dymouse

This uses a Raspberry and a Flask script to create an API for a USB postage scale. Python provides a means for grabbing the weight reading from the postage scale; Flask turns it into JSON; and Javascript turns the JSON into HTML text/plots.