Skip to main content

Twirpy

Python implementation of Twirp RPC framework (supports Twirp Wire Protocol v7).

This repo contains a protoc plugin that generates sever and client code and a pypi package with common implementation details.

For details about the twirp project, check https://github.com/twitchtv/twirp

Installation

Grab the protoc plugin to generate files with

go get -u github.com/verloop/twirpy/protoc-gen-twirpy

Add the twirp package to your project

pip install twirp

You'll also need uvicorn to run the server.

Generate and run

Use the protoc plugin to generate twirp server and client code.

We'll assume familiarity with the example from the docs. https://twitchtv.github.io/twirp/docs/example.html

protoc --python_out=./ --twirpy_out=./ ./haberdasher.proto

Server code

# server.py
import random

from twirp.asgi import TwirpASGIApp
from twirp.exceptions import InvalidArgument

from . import haberdasher_twirp, haberdasher_pb2

class HaberdasherService(object):
    def MakeHat(self, context, size):
        if size.inches <= 0:
            raise InvalidArgument(argument="inches", error="I can't make a hat that small!")
        return haberdasher_pb2.Hat(
            size=size.inches,
            color= random.choice(["white", "black", "brown", "red", "blue"]),
            name=random.choice(["bowler", "baseball cap", "top hat", "derby"])
        )


# if you are using a custom prefix, then pass it as `server_path_prefix`
# param to `HaberdasherServer` class.
service = haberdasher_twirp.HaberdasherServer(service=HaberdasherService())
app = TwirpASGIApp()
app.add_service(service)

Run the server with

uvicorn twirp_server:app --port=3000

Client code

# client.py
from twirp.context import Context
from twirp.exceptions import TwirpServerException

from . import haberdasher_twirp, haberdasher_pb2

client = haberdasher_twirp.HaberdasherClient("http://localhost:3000")

# if you are using a custom prefix, then pass it as `server_path_prefix`
# param to `MakeHat` class.
try:
    response = client.MakeHat(ctx=Context(), request=haberdasher_pb2.Size(inches=12))
    print(response)
except TwirpServerException as e:
    print(e.code, e.message, e.meta, e.to_dict())

Twirp Wire Protocol (v7)

Twirpy generates the code based on the protocol v7. This is a breaking change from the previous v5 and you can see the changes here.

This new version comes with flexibility to use any prefix for the server URLs and defaults to /twirp. To use an empty prefix or any custom prefix like /my/custom/prefix, pass it as a server_path_prefix param to server and clients. Check the example directory, which uses /twirpy as a custom prefix.

If you want to use the server and clients of v5, then use the 0.0.1 release.

Message Body Length

Currently, message body length limit is set to 100kb, you can override this by passing max_receive_message_length to TwirpASGIApp constructor.

# this sets max message length to be 10 bytes
app = TwirpASGIApp(max_receive_message_length=10)

Support and community

Python: #twirp. Join Python community slack here

Go: #twirp. Join Gophers community slack here

Standing on the shoulders of giants

Metadata

Release files for twirp 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twirp 0.0.7
File Size Uploaded
twirp-0.0.7.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for twirp 0.0.7
File Interpreter ABI Platform
twirp-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 23.8 kB

Release files / twirp-0.0.7.tar.gz

Download URL twirp-0.0.7.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
692b055d5da6e8ec29ded5dc93682c3ed5ec608bb9cb14529139a3b31cc2a44a
BLAKE2b-256 checksum
How to use checksums
4ce1ebb06fa8f7164ff7f81d5cd78a74b768373480c417b2837d94a5943872b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / twirp-0.0.7-py3-none-any.whl

Download URL twirp-0.0.7-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
978294787f07d9f5489e6e32ef050d4d8b619bdffcbe80b248797518b4f69871
BLAKE2b-256 checksum
How to use checksums
f5a812092a50daa826b692e79a199c3f55921ce5a9cd67bed8e454bcdbbf9fdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page