Skip to main content
File added late

1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.

This project is no longer active. See https://github.com/thriftrw/thriftrw-python/issues/164.

thriftrw

test coverage docs

thriftrw is a Python library to serialize and deserialize Thrift types.

Documentation is available on Read The Docs.

Features

  • No code generation. The .thrift files are parsed and compiled in-memory at runtime.

  • No assumptions about how bytes are sent or received. The library concerns itself with serialization and deserialization only.

  • Forward and cyclic references in types.

Example

Given the .thrift file,:

// blog.thrift

typedef string PostUUID

typedef binary RichText

union Body {
    1: string plainText
    2: RichText richText
}

struct PostDetails {
    1: required string author
    2: required string subject
    3: required Body body
}

exception UnauthorizedRequestError {
}

service BlogService {
    PostUUID newPost(1: PostDetails post)
        throws (1: UnauthorizedRequestError unauthorized);
}

You can use the library to send and receive requests and responses like so,

# client.py

import thriftrw

blog = thriftrw.load('blog.thrift')
BlogService = blog.BlogService

def new_post():
    post = blog.PostDetails(
        author='...',
        subject='...',
        body=blog.Body(plainText='Hello, world!')
    )

    request = BlogService.newPost.request(post)
    payload = blog.dumps(request)

    # send_to_server is implemented by the user.
    response_payload = send_to_server(payload)
    response = blog.loads(BlogService.newPost.response, response_payload)
    if response.unauthorized is not None:
        raise response.unauthorized
    else:
        return response.success
# server.py

import thriftrw

blog = thriftrw.load('blog.thrift')
BlogService = blog.BlogService

# The user's server handler calls handle_new_post with the payload.
def handle_new_post(request_payload):
    request = blog.loads(BlogService.newPost.request, request_payload)
    if request.post.author != 'admin':
        response = BlogService.newPost.response(
            unauthorized=blog.UnauthorizedRequestError()
        )
    else:
        # create_post is implemented by the user.
        post_uuid = create_post(request.post)
        response = BlogService.newPost.response(success=post_uuid)

    return blog.dumps(response)

Message Envelopes

Note that this example sends and receives just the request/response payload. It does not wrap the payload in a message envelope as expected by Apache Thrift. If you want to send or receive standard Apache Thrift requests to talk to other Apache Thrift services, you have to use the loads.message and dumps.message APIs. For example,

# client.py

def new_post():
    post = blog.PostDetails(...)
    request = BlogService.newPost.request(post)
    payload = blog.dumps.message(request)
    # ^ Instead of using blog.dumps, we use blog.dumps.message to indicate
    # that we want the request wrapped in a message envelope.


    response_payload = send_to_server(payload)

    # Similarly, instead of using blog.loads, we use blog.loads.message to
    # indicate that we want to parse a response stored inside a message.
    response_message = blog.loads.message(BlogService, response_payload)
    response = response_message.body

    if response.unauthorized is not None:
        raise response.unauthorized
    else:
        return response.success
# server.py

def handle_request(request_payload):
    message = blog.loads.message(BlogService, request_payload)
    if message.name == 'newPost':
        request = message.body
        # ...
        response = BlogService.newPost.response(success=post_uuid)
        return blog.dumps.message(response, seqid=message.seqid)
        # As before, we use blog.dumps.message instead of blog.dumps.
        # It is important that the server use the same seqid in the
        # response as what was used in the request, otherwise the client
        # will not be able to process out-of-order responses.
    else:
        raise Exception('Unknown method %s' % message.name)

For more information, see Overview.

Caveats

  • Only the Thrift Binary protocol is supported at this time.

License

Copyright (c) 2015 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Metadata

Release files for thriftrw 1.9.0

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

File added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for thriftrw 1.9.0
File Size Uploaded
thriftrw-1.9.0.tar.gz 1.5 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for thriftrw 1.9.0
File Interpreter ABI Platform
thriftrw-1.9.0-cp311-cp311-macosx_13_0_arm64.whl CPython 3.11 CPython 3.11 macOS 13.0+ ARM64 Details
thriftrw-1.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details

Total release size: 8.9 MB

Release files / thriftrw-1.9.0.tar.gz

Download URL thriftrw-1.9.0.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
e2563146a8cfd1cd581a46d87f5866186adb2f979f232c86f5f03fcf3b60f029
BLAKE2b-256 checksum
How to use checksums
e57e6ce8e12e12a7524c0884963d8512c9160dc13ec9965804b49df99238dbb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / thriftrw-1.9.0-cp311-cp311-macosx_13_0_arm64.whl

Download URL thriftrw-1.9.0-cp311-cp311-macosx_13_0_arm64.whl
Size 880.6 kB
Tags CPython 3.11 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
58965b5ab94ebde4cbed4e40b35729a98c9ed52fce676b0889be6cf18751f627
BLAKE2b-256 checksum
How to use checksums
98129fb03372474bc3df24b670d3dc261516ab7a390a6e14eb42e9e0184625b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / thriftrw-1.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL thriftrw-1.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.5 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f9124b1ea73ce76654f99a7ae15246b39015ab091d75b18fe60c9808115c1b83
BLAKE2b-256 checksum
How to use checksums
cb2719387d7c3feb5b7a6ee26f3edf55848a13a3493da5d5caee1ed2bd7cb3c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.9.0 This release

3 release files

1.8.1

2 release files

1.8.0

2 release files

1.7.2

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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