I have many linux systems on my network. When I first started doing Raspberry Pi projects, I just added new features to an existing server. For example, my garage door opener and security system ran on the same Raspberry Pi. This was convenient, but bad practice (i.e., poor architectural design). So, now that I am trying to do it right, I need a way for servers to communicate. RESTful APIs are the current best practice.
The goal of this project is to provide server-side and client-side python3 scripts, directories and json files. The scripts should be full-featured, well-documented, and therefore easily understood and modified for your applications.
In addition, I am trying to use as many best practices as possible as described by W3schools. Initially, these scripts didn't do anything useful. After working on it for a while, I decided every Raspberry Pi should have a portion of the API dedicated to providing stats about the server. So, now they do something useful.
An API provides a basic set of services: add (post), read (get), edit (put), and delete (delete). The http(s) service names are in the parentheses.
A client-server script with appropriate privileges can use those services via an API on the server-side script. So, for my security system and garage door opener, the security system can query if the garage door is opened or closed, and then based on the security system settings alert me the door is open when it sh
An API provides data and resources to a caller. The API has the following requirements:
Only other servers on my LAN can use the API
The servers must establish an SSL connection prior to passing data
An API should be able to handle multiple simultaneous requests (multi-threading)
Prerequisites:
Two Raspberry Pis setup and running latest version of Raspberry Pi OS (nee raspbian).
One server is the client-side and the other is the server-side. The server-side supplies its data to the client.
Step 1. Add requests library
Open a terminal window and login to each raspberry pi and run:
$ pip install requests
$ sudo pip3 install distro
$ pip3 install distro
and this will show all available methods and attributes. Check if it works
$ python3
>>> import requests
>>> dir(requests)
>>> exit()
Step 2. Edit /etc/hosts
This is an update. I added an apache2 webserver to the server running the API and ran into issues. The port was open and the application was listening on the port, but all attempts to connect from another server were rejected.
The following fixed the issue:
$ sudo nano /etc/hosts
If there is a line like:
127.0.1.1 ♣host-name♣
Comment it out by adding # at the front and then add the line below:
# 127.0.1.1 ♣host-name♣
0.0.0.0 ♣host-name♣
Step 3. Enable and allow port in ufw [optional]
ufw is an uncomplicated firewall, which I add to strengthen the security of my Raspberry Pis.
Pick a port number. Choose a high numbered port. In this guide, 9080 is used for http and 9433 is used for https. Pick another port n umber: ♣port♣
To allow the port number to pass through the firewall, run the commands:
$ sudo ufw enable
$ sudo ufw status
$ sido ufw allow ♣port♣
If ufw is not present or an unknown command, then you can ignore this step.