Registry / web-framework / cornice

cornice

JSON →
library6.1.0pypypi✓ verified 89d ago

Cornice is a Python library that simplifies the definition of RESTful web services within the Pyramid web framework. It allows developers to declare services, resources, and their methods using decorators and schema validation, often integrating with tools like Colander and Marshmallow. The current version is 6.1.0, and it maintains an active release cadence, frequently updating for Python and Pyramid compatibility and addressing dependencies.

pip install cornice
INSTALL
IMPORT
SIG · CORNICE
C
cornice
web-frameworkpythonv6.1.0
Install
2.8s avg
Import
592ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v6.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.635s · 24.3MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 2.8s · import 0.550s · 25MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Service
✓ from cornice import Service
✗ from cornice.service import Service
Service is directly exposed at the top level of the cornice package.
resource
✓ from cornice.resource import resource
Used to define resources with multiple services and CRUD operations.
validators
✓ from cornice import validators
Access to built-in validators, often used with Colander schemas.

This quickstart demonstrates how to define a simple 'Hello World' service using Cornice within a standalone Pyramid application. Run this script, then access `http://0.0.0.0:6543/hello` in your browser or with `curl`.

from wsgiref.simple_server import make_server from pyramid.config import Configurator from cornice import Service # 1. Define a Cornice service hello_service = Service(name='hello', path='/hello', description="Hello World Service") @hello_service.get() def get_hello(request): """Returns a 'Hello World!' message.""" # Access request data via request.GET, request.json, request.validated, etc. return {'message': 'Hello World! from Cornice!'} # 2. Integrate into a Pyramid application if __name__ == '__main__': with Configurator() as config: # Include the cornice package to register its components config.include('cornice') # Add the defined Cornice service to the Pyramid configuration config.add_cornice_service(hello_service) # Create a WSGI application app = config.make_wsgi_app() # 3. Run the Pyramid application server = make_server('0.0.0.0', 6543, app) print('Serving on http://0.0.0.0:6543/hello') print('Press Ctrl+C to quit') try: server.serve_forever() except KeyboardInterrupt: pass finally: server.server_close()
Debug
Known issues
breakingCornice 6.0.0 dropped support for Marshmallow versions older than 3. Attempting to use an older Marshmallow version will lead to compatibility issues or errors.
fix
Upgrade Marshmallow to version 3 or higher using `pip install 'marshmallow>=3.0.0'`.
affects: >=6.0.0
breakingCornice 5.0.0 dropped support for Python 2.x. Applications running on Python 2 will not be compatible with Cornice versions 5.0.0 and above.
fix
Migrate your application codebase to Python 3.x to use Cornice 5.0.0 or newer.
affects: >=5.0.0
gotchaIn Cornice 5.0.2, the internal default JSON renderer name changed from `cornice` to `cornicejson`. While this change was not officially documented as a breaking change, applications explicitly relying on or configuring a renderer named 'cornice' might experience issues.
fix
If you had `config.add_renderer('cornice', 'some.renderer.path')` or similar, update it to use `cornicejson` if you relied on the implicit name, or ensure your renderer configuration explicitly maps to the desired name.
affects: >=5.0.2
gotchaPrior to Cornice 5.0.3, Colander validators (e.g., `body_validator`) might incorrectly miss `body`, `headers`, `path`, or `querystring` data from the request, leading to incomplete validation.
fix
Upgrade to Cornice 5.0.3 or higher to ensure Colander validators receive all expected request data for accurate validation.
affects: <5.0.3
Errors
Common errors & fixes
ImportError: cannot import name 'Service' from 'cornice'
Incorrect import path; 'Service' is directly available in the top-level 'cornice' package, not a submodule.
fix
Change the import to `from cornice import Service`.
AttributeError: 'Service' object has no attribute 'add_get'
Attempting to use older, deprecated `add_` methods (e.g., `add_get`, `add_post`) instead of the decorator syntax for defining service methods.
fix
Use the decorator syntax for defining service methods, e.g., `@service.get()` or `@service.post()`.
TypeError: 'Request' object is not subscriptable
Attempting to access request attributes like `request['json']` or `request['validated']` using dictionary-style bracket notation.
fix
Access request data using dot notation: `request.json` or `request.validated`.
colander.Invalid: ... missing or invalid fields ...
Validation failed because the request body or parameters do not conform to the defined Colander schema. This often indicates missing required fields or incorrect data types.
fix
Review your Colander schema definition and ensure the incoming request data (e.g., JSON body, query parameters) matches the expected structure and types. Check logs for specific field errors.
Upgrade
Version history
6.1.0latest on PyPI · released Feb 8, 2024
Audit
Dependencies
pyramidrequiredCore web framework integration.
colanderoptionalOptional, for declarative schema validation of request data.
marshmallowoptionalOptional, for declarative schema validation of request data. Requires Marshmallow >= 3.0 since Cornice 6.0.0.
Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
1
Resources
cornice — pip install cornice · libregistry