Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Definieren Sie einen API-Server, der die von Ihnen erstellte(n) API(s) verwendet.
Cloud Endpoints Frameworks für Python implementiert die standardmäßige Schnittstelle Web Server Gateway Interface (WSGI), um Anforderungen an die API für die Methoden im Code weiterzuleiten.
Wie bei jeder in App Engine ausgeführten Anwendung müssen Sie eine Datei namens app.yaml erstellen, in der Sie die Einstellungen der App Engine-Anwendung konfigurieren. Wenn Sie den Webserver definieren möchten, müssen Sie Änderungen in der Datei app.yaml vornehmen.
Einen Webserver definieren:
Erstellen Sie ein Python-Modul wie main.py und auf der obersten Ebene das Objekt endpoints.api_server:
api=endpoints.api_server([EchoApi])
Der Code api = endpoints.api_server([EchoApi]) erstellt eine WSGI-Anwendung, mit der API-Anfragen zu den Methoden in der Klasse EchoAPI weitergeleitet werden.
Sie können eine Liste von remote.Service-Objekten, die Sie beim Erstellen der API definiert haben, für endpoints.api_server bereitstellen. Wenn die API in mehreren Klassen implementiert ist, dann ist das Objekt remote.Service eine Sammlung von Klassen, wie unter Auf mehrere Klassen abgestimmte API erstellen beschrieben.
Das Erstellen eines eigenen Moduls für das Objekt endpoints.api_server hängt davon ab, ob Sie eine einzelne API oder mehrere APIs erstellt haben.
Wenn Sie mehrere APIs (mit mehreren untergeordneten Klassen remote.Service) erstellt haben und diese APIs in mehreren Dateien definiert sind, sollten Sie ein eigenes Modul für das Objekt endpoints.api_server erstellen, damit Sie alle Klassendateien importieren können.
Wenn Sie eine einzelne API erstellt haben, können Sie den erforderlichen Code endpoints.api_server in das Modul einfügen, in dem Sie Ihre API definieren, da Sie keine anderen Klassen importieren müssen.
Ordnen Sie in der Datei app.yaml den gerade erstellen Webserver dem Cloud Endpoints-Standort so zu:
handlers:# The endpoints handler must be mapped to /_ah/api.-url:/_ah/api/.*script:main.api
Dabei ist main das Python-Modul, in dem Sie das Objekt endpoints.api_server definiert haben.
API über anderen Pfad bereitstellen
Optional: So stellen Sie die API über einen anderen Pfad (z. B. /api/) bereit:
Ändern Sie in der Datei app.yaml den Abschnitt handlers:
handlers:-url:/api/.*script:main.api
Logging mit Endpoint Frameworks for Python
In Endpoints Frameworks for Python wird das standardmäßige Python-Logging-Modul verwendet, um Informationen zum Status der Anwendung und zum Lebenszyklus der Anfrage zu protokollieren. Weitere Informationen zu App Engine-Logs und wie sie angezeigt werden, finden Sie in der Dokumentation zu App Engine unter Anwendungslogs lesen und schreiben.
Das Python-Logging-Modul bietet vordefinierte Logebenen. In aufsteigender Reihenfolge der Schweregrade lauten die Logebenen so:
Logebene
Beschreibung
DEBUG
Bietet detaillierte Logs. Normalerweise wird diese Stufe nur zur Fehlerbehebung festgelegt.
INFO
Die standardmäßige Logebene für Endpoints Frameworks. Ermöglicht das Verfolgen des Fortschritts Ihrer Anwendung auf übergeordneter Ebene.
WARNING
Warnt Sie bei unerwarteten Ereignissen. Die Anwendung kann jedoch wiederhergestellt und trotzdem normal ausgeführt werden.
ERROR
Warnt Sie bei einem Fehler, der zu Funktionsverlust führen kann, obwohl die Anwendung weiterhin ausgeführt wird.
CRITICAL
Warnt Sie bei einem schwerwiegenden Fehler oder Ereignis, wodurch die Anwendung möglicherweise heruntergefahren wird.
Logger existieren in einer durch Punkte getrennten Hierarchie. Der Logger endpoints.api_config ist beispielsweise dem Logger endpoints untergeordnet. Mit dieser Hierarchie können Sie genau steuern, welche Logs ausgegeben oder unterdrückt werden. In der Regel ändern Sie nur die beiden Stamm-Logger für Endpoints Frameworks: endpoints und endpoints_management.
DEBUG-Logging für Endpoints Frameworks aktivieren
Endpoints Frameworks legt die Logger so fest, dass nur Logeinträge mit der Logebene INFO oder höher aufgezeichnet werden, damit der Logreader nicht überlastet wird. Nachdem Endpoints Frameworks in das Modul importiert wurde, können Sie die Logebenen jederzeit so ändern:
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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."]]