Configure Python agent middleware
Use an installation tool for most applications. The Contrast Runner, the Agent Operator and the Flex Agent each load the agent early in your application's startup and add middleware for the frameworks that Contrast supports.
Add middleware by hand only when your application uses a WSGI or ASGI framework that your installation tool does not detect. Use manual middleware in addition to an installation tool, not in place of one.
Without an installation tool, the agent starts too late in your application's startup, so Assess is less accurate. Protect is currently unaffected, but this can change. If you cannot use an installation tool, contact Contrast Support.
The agent logs a warning that explicit middleware is no longer necessary. To stop the warning when you need manual middleware, set agent.python.enable_automatic_middleware to false in your configuration file, or set the environment variable CONTRAST__AGENT__PYTHON__ENABLE_AUTOMATIC_MIDDLEWARE to false.
For Flask, Pyramid, Quart, and FastAPI, the framework-specific imports such as contrast.fastapi.ContrastMiddleware are aliases of the generic middleware. Use the generic middleware, and see your framework's documentation for how to add a WSGI or ASGI middleware to your application.
WSGI
Contrast provides a generic WSGI middleware that includes all core agent functionality. Route discovery works for the frameworks on the Python supported technologies page, except Django, which needs the Django middleware below.
To instrument any WSGI-compliant application with Contrast, find the WSGI application object in your application codebase. Replace the placeholder with your own application object.
from example.module import make_app from contrast.wsgi import ContrastMiddleware wsgi_app = ContrastMiddleware(make_app())
See the WSGI specification for additional details on WSGI middleware.
ASGI
Contrast provides a generic ASGI middleware that includes core agent functionality but no framework-specific features. Route discovery works for the frameworks on the Python supported technologies page, except Django, which needs the Django middleware below.
To instrument an ASGI-compliant HTTP/HTTPS application, find the ASGI application object in your codebase. Replace the placeholder with your own application object:
from example.module import make_app from contrast.asgi import ContrastMiddleware asgi_app = ContrastMiddleware(make_app())
Django
Use the Django middleware for Django applications, not the generic middleware. It also sets up Django route discovery, Django configuration checks, and the application name.
For a WSGI application, wrap the application object in wsgi.py:
from django.core.wsgi import get_wsgi_application from contrast.django import ContrastMiddleware application = ContrastMiddleware(get_wsgi_application())
For an ASGI application, wrap the application object in asgi.py:
from django.core.asgi import get_asgi_application from contrast.django_asgi import ContrastMiddleware application = ContrastMiddleware(get_asgi_application())
AIOHTTP
The runner adds the AIOHTTP middleware for you. Add it yourself only when you pass middlewares to web.Application as a tuple or another type that is not a list.