Introduzione a Falcon

Mattepuffo's logo
Introduzione a Falcon

Introduzione a Falcon

Falcon è un framework per Python per la creazione di API Web e micorservice.

Agisce a basso livello rispetto ad altri framework del genere, e per questo risulta molto veloce.

Vediamo come creare un semplice esempio per fare un test.

Dentro alla cartella del nostro progetto:

(venv) $ pip3 install falcon gunicorn

Come vedete ho usato un ambiente virtuale.

Questo lo script:

import falcon

class Test:
    def on_get(self, req, resp):
        book = {
            'title': 'IT',
            'author': 'Stepehn King'
        }

        resp.media = book


api = falcon.API()
api.add_route('/test', Test())

Per avviare il programma, sempre da dentro la cartella del progetto:

(venv) $ gunicorn main:api
[2020-02-19 14:15:04 +0100] [27370] [INFO] Starting gunicorn 20.0.4
[2020-02-19 14:15:04 +0100] [27370] [INFO] Listening at: http://127.0.0.1:8000 (27370)
[2020-02-19 14:15:04 +0100] [27370] [INFO] Using worker: sync
[2020-02-19 14:15:04 +0100] [27373] [INFO] Booting worker with pid: 27373

Dove main è il nome delo script (main.py) e api è il nome della istanza delle nostre api.

A questo punto andate su http://127.0.0.1:8000/test per vedere il risultato.

Enjoy!


Condividi

Commentami!