Understand data manipulation and programming interfaces in Python 3 through an application to robotics: the calculation of the Euclidean distance between two points in a 3-dimensional system with Falcon, Gunicorn and Insomnia.
Deploying a REST API in Python on a remote server can be a difficult step in producing a functional application. The purpose of this article is to propose an extremely simplified code, of the configuration of a Falcon module, in particular for the beginners in web programming wishing to put at their disposal tools of remote calculation. In ten minutes, your first server will be operational, despite basic knowledge of Python 3. To make this article even more attractive, let’s look at a real problem, that of positioning in space and calculating distances between two points in a three-dimensional frame of reference. Useful in the context of a modeling or robotics project, the program below will allow the reception of a JSON request, and the sending of a response in the same format, after application of a mathematical theorem on a remote machine, equipped with a Python 3.10 environment with updated libraries.
Compatibility test performed with Python 3.10, executed on MacOS 11.5.1 and Linux Ubuntu Server 20.04 LTS environments.
Libraries Used : Falcon, Gunicorn, Math, Json.
pip3 install falcon gunicorn
The source code will first observe the presence of the libraries necessary to its execution, then, download them in case of absence in order to automatically update the software configuration of the execution environment.
In order to make the program executable with the minimum of human intervention, and to optimize its deployment on the server, an autonomy of upgrade, data download and exception handling has been implemented, notably within the server_SMTP class.
First, it is necessary to initialize our Gunicorn server, on a listening port not used by the system, in our case, port 5000 seems a perfect candidate and should not cause any inconvenience. To do this, enter the following command in a terminal window :
gunicorn -b 0.0.0:5000 main — reload
Note : It is essential to name your file main.py for the execution of Gunicorn, a different name will cause an exception and will not be able to reach a server launch.
Then, you have to configure your application in the main.py file, using a set of variables pointing to the Falcon library.
api = app = application = falcon.API()
From this point on, you…
Continue reading: https://towardsdatascience.com/the-simplest-way-to-build-a-rest-api-with-falcon-and-python-3-10-application-in-spatial-geometry-c1ee305aed75?source=rss—-7f60cf5620c9—4