Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Devi definire un web server che utilizzi l'API o le API che hai creato.
Cloud Endpoints Frameworks per Python implementa l'interfaccia gateway server web (WSGI) standard per indirizzare le richieste alla tua API ai metodi nel tuo codice.
Come per ogni applicazione eseguita su App Engine, devi creare un file
denominato app.yaml in cui
configurare le impostazioni dell'app App Engine. Per definire il server web,
apporta modifiche al file app.yaml.
Per definire un server web:
Crea un modulo Python, ad esempio main.py, e crea un oggetto
endpoints.api_server di primo livello:
api=endpoints.api_server([EchoApi])
Il codice api = endpoints.api_server([EchoApi]) crea un'applicazione WSGI
che indirizza le richieste API ai metodi della classe EchoAPI.
Puoi fornire un elenco di oggetti remote.Service (che hai definito quando hai creato l'API) per endpoints.api_server. Se hai un'API implementata in più classi, l'oggetto remote.Service è una raccolta di classi come descritto in Creazione di un'API implementata con più classi.
La creazione di un modulo separato per l'oggetto endpoints.api_server
dipende dalla creazione di una singola API o di più API.
Se hai creato più API (utilizzando più sottoclassi remote.Service) definite in più file, ti consigliamo di creare un modulo separato per l'oggetto endpoints.api_server in modo da poter importare tutti i file di classe.
Se hai creato una singola API, puoi aggiungere il codice
endpoints.api_server richiesto al modulo in cui definisci l'API
perché non devi importare altre classi.
Nel file app.yaml, mappa il server web che hai appena creato nella posizione di Cloud Endpoints nel seguente modo:
handlers:# The endpoints handler must be mapped to /_ah/api.-url:/_ah/api/.*script:main.api
dove main è il modulo Python in cui hai definito l'oggetto
endpoints.api_server.
Gestione dell'API da un percorso diverso
(Facoltativo) Per pubblicare l'API da un percorso diverso, ad esempio /api/:
Endpoints Frameworks per Python utilizza il
modulo di logging Python standard
per registrare informazioni su
lo stato dell'applicazione e il ciclo di vita delle richieste. Per scoprire di più sui log di App Engine e su come visualizzarli, consulta Lettura e scrittura dei log delle applicazioni nella documentazione di App Engine.
Il modulo di logging Python fornisce livelli di log predefiniti. In ordine crescente
di gravità, i livelli di log sono:
Livello di log
Descrizione
DEBUG
Fornisce log dettagliati. In genere, questo livello viene impostato solo durante la risoluzione di un problema.
INFO
Il livello di log predefinito per Endpoints Frameworks. Consente di tracciare l'avanzamento della richiesta a livello generale.
WARNING
Ti avvisa che si è verificato un problema imprevisto, ma l'applicazione può ripristinarsi e continuare a funzionare normalmente.
ERROR
Ti avvisa che si è verificato un errore che potrebbe causare una perdita di funzionalità, ma l'applicazione è ancora in esecuzione.
CRITICAL
Ti avvisa che si è verificato un errore o un evento grave che potrebbe causare l'arresto dell'applicazione.
I logger esistono in una gerarchia separata da punti. Ad esempio, il logger
endpoints.api_config è un elemento figlio del logger endpoints. Questa gerarchia
ti consente di controllare con precisione quali log vengono emessi o eliminati. In genere,
modifica solo i due logger radice per Endpoints Frameworks:
endpoints e endpoints_management.
Attiva la registrazione di DEBUG per Endpoints Frameworks
Per evitare di sovraccaricare il lettore di log, Endpoints Frameworks imposta i propri
logger in modo che registrino solo le voci di log con il livello di log INFO o superiore. In qualsiasi
momento dopo l'importazione di Endpoints Frameworks nel modulo,
puoi modificare il livello di log nel seguente modo:
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eA web server must be defined within the \u003ccode\u003eapp.yaml\u003c/code\u003e file to route requests to your API's methods using the Web Server Gateway Interface (WSGI).\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eendpoints.api_server\u003c/code\u003e object, created in a Python module like \u003ccode\u003emain.py\u003c/code\u003e, functions as a WSGI application that routes API requests, with the option to include multiple \u003ccode\u003eremote.Service\u003c/code\u003e objects if you have a multi-class API.\u003c/p\u003e\n"],["\u003cp\u003eIn the \u003ccode\u003eapp.yaml\u003c/code\u003e file, you must map the web server to the Cloud Endpoints location, by setting the url handler to \u003ccode\u003e/_ah/api/.*\u003c/code\u003e or a different path if configured, and link the script to the module where \u003ccode\u003eendpoints.api_server\u003c/code\u003e was defined.\u003c/p\u003e\n"],["\u003cp\u003eEndpoints Frameworks for Python employs the standard Python logging module, with levels ranging from \u003ccode\u003eDEBUG\u003c/code\u003e to \u003ccode\u003eCRITICAL\u003c/code\u003e, defaulting to \u003ccode\u003eINFO\u003c/code\u003e, and can be modified using the \u003ccode\u003esetLevel\u003c/code\u003e method for loggers like \u003ccode\u003eendpoints\u003c/code\u003e and \u003ccode\u003eendpoints_management\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Creating a web server\n\nYou must define a web server that uses the\n[API or APIs you have created](/endpoints/docs/frameworks/python/create_api).\nCloud Endpoints Frameworks for Python implements the standard\n[Web Server Gateway Interface](https://wsgi.readthedocs.io/)\n(WSGI) to route requests to your API to the methods in your code.\n\nAs with every application that runs on App Engine, you must create a file\ncalled [`app.yaml`](/appengine/docs/standard/python/config/appref) in which you\nconfigure your App Engine app's settings. To define the web server,\nyou make changes to your `app.yaml` file.\n\nTo define a web server:\n\n1. Create a Python module, for example `main.py`, and create an\n `endpoints.api_server` object at the top-level:\n\n api = endpoints.api_server([EchoApi])\n\n The code `api = endpoints.api_server([EchoApi])` creates a WSGI application\n that routes API requests to the methods in the `EchoAPI` class.\n\n You can provide a list of `remote.Service` objects (which you defined when\n you [created your API](/endpoints/docs/frameworks/python/create_api)) to\n `endpoints.api_server`. If you have an API that is implemented in several\n classes, then your `remote.Service` object is a collection of classes as\n described in [Creating an API implemented with multiple\n classes](/endpoints/docs/frameworks/python/create-multi-class-api).\n\n Whether you create a separate module for the `endpoints.api_server` object\n depends on whether you created a single API or multiple APIs.\n - If you created multiple APIs (using multiple `remote.Service`\n subclasses) that are defined in multiple files, then we recommend that\n you create a separate module for the `endpoints.api_server` object so\n that you can import all the class files.\n\n - If you created a single API, you can add the required\n `endpoints.api_server` code to the module where you define your API\n because you don't need to import any other classes.\n\n | **Warning:** if your code doesn't call `endpoints.api_server`, you will get a deployment failure when you try to deploy.\n2. In your `app.yaml` file, map the web server you just created to\n the Cloud Endpoints location as follows:\n\n handlers:\n # The endpoints handler must be mapped to /_ah/api.\n - url: /_ah/api/.*\n script: main.api\n\n where `main` is the Python module you in which you defined the\n `endpoints.api_server` object.\n\nServing your API from a different path\n--------------------------------------\n\nOptional: To serve your API from a different path, for example `/api/`:\n\n1. Modify the decorator:\n\n @endpoints.api(name='echo', version='v1', base_path='/api/')\n\n2. Change the `handlers` section in the `app.yaml` file:\n\n handlers:\n - url: /api/.*\n script: main.api\n\nLogging in Endpoints Frameworks for Python\n------------------------------------------\n\nEndpoints Frameworks for Python uses the\n[standard Python logging module](https://docs.python.org/2/library/logging.html)\nto log information about\nthe application's status and request lifecycle. To learn more about\nApp Engine logs and how to view them, review\n[Reading and writing application logs](/appengine/docs/standard/python/logs#writing_application_logs)\nin the App Engine documentation.\n\nThe Python logging module provides predefined log levels. In increasing order\nof severity, the log levels are:\n\nLoggers exist in a dot-separated hierarchy. For example, the logger\n`endpoints.api_config` is a child of the logger `endpoints`. This hierarchy\ngives you precise control over which logs are emitted or suppressed. Typically,\nyou only change the two root loggers for Endpoints Frameworks:\n`endpoints` and `endpoints_management`.\n\n### Enable `DEBUG` logging for Endpoints Frameworks\n\nTo avoid overloading the log reader, Endpoints Frameworks sets its\nloggers to only record log entries with the `INFO` log level or higher. At any\ntime after Endpoints Frameworks has been imported into your module,\nyou can change the log level as follows: \n\n import logging\n logging.getLogger('endpoints').setLevel(logging.DEBUG)\n logging.getLogger('endpoints_management').setLevel(logging.DEBUG)\n\nThe `setLevel` method sets the minimum log level for the logger."]]