Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python SDK for CloudEvents

License

NotificationsYou must be signed in to change notification settings

cloudevents/sdk-python

Repository files navigation

PyPI version

Status

This SDK is still considered a work in progress, therefore things might (andwill) break with every update.

This SDK current supports the following versions of CloudEvents:

  • v1.0
  • v0.3

Python SDK

Packagecloudevents provides primitives to work with CloudEvents specification:https://github.com/cloudevents/spec.

Installing

The CloudEvents SDK can be installed with pip:

pip install cloudevents

Sending CloudEvents

Below we will provide samples on how to send cloudevents using the popularrequests library.

Binary HTTP CloudEvent

fromcloudevents.httpimportCloudEventfromcloudevents.conversionimportto_binaryimportrequests# Create a CloudEvent# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".attributes= {"type":"com.example.sampletype1","source":"https://example.com/event-producer",}data= {"message":"Hello World!"}event=CloudEvent(attributes,data)# Creates the HTTP request representation of the CloudEvent in binary content modeheaders,body=to_binary(event)# POSTrequests.post("<some-url>",data=body,headers=headers)

Structured HTTP CloudEvent

fromcloudevents.conversionimportto_structuredfromcloudevents.httpimportCloudEventimportrequests# Create a CloudEvent# - The CloudEvent "id" is generated if omitted. "specversion" defaults to "1.0".attributes= {"type":"com.example.sampletype2","source":"https://example.com/event-producer",}data= {"message":"Hello World!"}event=CloudEvent(attributes,data)# Creates the HTTP request representation of the CloudEvent in structured content modeheaders,body=to_structured(event)# POSTrequests.post("<some-url>",data=body,headers=headers)

You can find a complete example of turning a CloudEvent into a HTTP requestin the samples' directory.

Receiving CloudEvents

The code below shows how to consume a cloudevent using the popular python web frameworkflask:

fromflaskimportFlask,requestfromcloudevents.httpimportfrom_httpapp=Flask(__name__)# create an endpoint at http://localhost:/3000/@app.route("/",methods=["POST"])defhome():# create a CloudEventevent=from_http(request.headers,request.get_data())# you can access cloudevent fields as seen belowprint(f"Found{event['id']} from{event['source']} with type "f"{event['type']} and specversion{event['specversion']}"    )return"",204if__name__=="__main__":app.run(port=3000)

You can find a complete example of turning a CloudEvent into a HTTP requestin the samples' directory.

SDK versioning

The goal of this package is to provide support for all released versions of CloudEvents,ideally while maintaining the same API. It will use semantic versioningwith following rules:

  • MAJOR version increments when backwards incompatible changes is introduced.
  • MINOR version increments when backwards compatible feature is introducedINCLUDING support for new CloudEvents version.
  • PATCH version increments when a backwards compatible bug fix is introduced.

Community

Each SDK may have its own unique processes, tooling and guidelines, commongovernance related material can be found in theCloudEventsdocsdirectory. In particular, in there you will find information concerninghow SDK projects aremanaged,guidelinesfor how PR reviews and approval, and ourCode of Conductinformation.

If there is a security concern with one of the CloudEvents specifications, orwith one of the project's SDKs, please send an email tocncf-cloudevents-security@lists.cncf.io.

Additional SDK Resources

Maintenance

We useblack andisort for autoformatting. We set up atoxenvironment to reformat the codebase.

e.g.

pip install toxtox -e reformat

For information on releasing version bumps seeRELEASING.md

Packages

No packages published

Contributors24

Languages


[8]ページ先頭

©2009-2025 Movatter.jp