Step 9. Get the scripts and icon from github
Open a terminal window, ssh into the server raspberry pi and run the command:
$ cd ~/api
$ wget "https://raw.githubusercontent.com/dumbo25/restful_api/master/rest_server.py"
$ wget https://github.com/dumbo25/restful_api/blob/master/favicon.png
Open a second terminal window and ssh into the client raspberry pi and run the command:
$ cd ~/api
$ wget https://raw.githubusercontent.com/dumbo25/restful_api_client/master/rest_client.py
Changes to the cert path and cert names may be required.
Step 10. Try it
The server must be running before the client is run. The s & c options must be used or not used on both the server and the client scripts. Also, the port used must be the same for both scripts.
In the server window run the command to see help:
$ python3 rest_server.py -h
Start the server running with the command:
$ python3 rest_server.py
In the client window, run one of the following commands:
$ python3 rest_client.py -a/api/cpu
$ curl -X GET http://♣host-name♣:♣port♣/api/cpu
In general, the script should be run with the -msc option. Try it
Step 11. Make rest_server.py executable
In the server window, run the commands:
$ chmod +x rest_server.py
$ sudo cp rest_server.py /usr/bin/.
And try it using:
$ /usr/bin/rest_server.py [options]
Step 12. Make cron run the script and check if it is running
In the server window, download the cron shell script by running the command:
$ wget "https://raw.githubusercontent.com/dumbo25/restful_api/master/server_cron.sh"
Edit crontab using:
$ sudo crontab -e
And at the end of the file enter the line below, which will check if the script is running every five minutes and restart it if it is stopped:
*/5 * * * * bash /home/pi/api/server_cron.sh
Troubleshooting the connection:
To check if the port is open and being listened to on the API server, run the following commands:
$ python3 /home/pi/api/rest_server.py &
$ nc -zv ♣host-name♣ ♣port♣
Connection to ♣host-name♣ ♣port♣ port [tcp/*] succeeded!
then stop the rest_server.py process. Get the PID and then kill it
$ ps -aux | grep rest_server
and sudo kill the PID of the rest_server
To check if the port is open and accepting connections from another server try one of the following:
$ cat < /dev/null > /dev/tcp/♣host-name♣:♣port♣; echo $?
returns 1 if open and connected, otherwise returns 0
$ curl -X GET http://♣host-name♣:♣port♣/api/cpu
$ python3 rest_client.py -a/api/cpu
both curl and the python script return something like:
{"cores": "1", "frequency": "1.0GHz", "used": "3.6%"}