Skip to main content

Flask ThriftClient

Introduction

This extension provide a simple intergration with Thrift RPC server.

from flask import Flask
from flask_thriftclient import ThriftClient

from MyGeneratedThriftCode import MyService

app = Flask(__name__)
app.config["THRIFTCLIENT_TRANSPORT"] = "tcp://127.0.0.1:9090"

thriftclient = ThriftClient(MyService.Client, app)

@app.route("/")
def home():
    data = thriftclient.client.mymethod()
    return data

Transport

Thrift endpoints are defined in the configuration variable THRIFTCLIENT_TRANSPORT as an URL. The default transport is tcp://localhost:9090

Available url schemes are:

tcp: use TCP socket as transport, you have to define the server address and port. If the port isn’t defined, 9090 will be used

Example:

http: use HTTP protocol as transport. Examples:

unix: use unix sockets as transport, as this scheme follow URI format, it MUST have either no or three “/” before the socket path

  • unix:///tmp/mysocket #absolute path

  • unix:/tmp/mysocket #absolute path

  • unix:./mysocket #relative path

SSL

You may set SSL version of transport communications by using ‘s’ version of url scheme:

tcp <=> tcps http <=> https unix <=> unixs

examples:

Two options are related to SSL transport:

THRIFTCLIENT_SSL_VALIDATE: True if the certificate has to be validated (default True)

THRIFTCLIENT_SSL_CA_CERTS: path to the SSL certificate (default None)

Note that you MUST set one of theses options:

app.config["THRIFTCLIENT_SSL_VALIDATE"] = False
app.config["THRIFTCLIENT_TRANSPORT"] = "https://127.0.0.1/"

#or

app.config["THRIFTCLIENT_SSL_CA_CERTS"] = "./cacert.pem"
app.config["THRIFTCLIENT_TRANSPORT"] = "https://127.0.0.1/"

Protocol

You may define which procotol must be use by setting the parametter THRIFTCLIENT_PROTOCOL. The default protocol is Binary.

Available parametters are:

ThriftClient.BINARY or “BINARY” : use the binary protocol

ThriftClient.COMPACT or “COMPACT” : use the compact protocol

ThriftClient.JSON or “JSON” : use the JSON protocol. note that this protocol is only available for thrift >= 0.9.1

Connection

By default the application will open then close the transport for each request This can be overriden by setting THRIFTCLIENT_ALWAYS_CONNECT to False

when THRIFTCLIENT_ALWAYS_CONNECT is set to False there is 3 ways to handle your connections:

  • you can call transport.close and transport.open manually

  • you can use the autoconnect decorator

  • you can use the connect “with” context

app = Flask(__name__)
app.config["THRIFTCLIENT_TRANSPORT"] = "tcp://127.0.0.1:9090"
app.config["THRIFTCLIENT_ALWAYS_CONNECT"] = False

thriftclient = ThriftClient(MyService.Client, app)

@app.route("/with_autoconnect")
@thriftclient.autoconnect
def with_autoconnect():
    data = thriftclient.client.mymethod()
    return data

@app.route("/with_context")
def with_context():
    with thriftclient.connect():
        data = thriftclient.client.mymethod()
        return data

@app.route("/with_manual_connection")
def with_manual_connection():
    thriftclient.transport.open()
    data = thriftclient.client.mymethod()
    thriftclient.transport.close()
    return data

Options

Other options are:

THRIFTCLIENT_BUFFERED: use buffered transport (default False)

THRIFTCLIENT_ZLIB: use zlib compressed transport (default False)

Metadata

Release files for Flask-ThriftClient 0.2.0

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

Source distribution (sdist)

Source distribution for Flask-ThriftClient 0.2.0
File Size Uploaded
Flask-ThriftClient-0.2.0.tar.gz 5.7 kB Details

Release files / Flask-ThriftClient-0.2.0.tar.gz

Download URL Flask-ThriftClient-0.2.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
be0a1fee3c31a4579eebbefab6ac4bcd64f44090aa8e3bacd6c9d8735bbefa33
BLAKE2b-256 checksum
How to use checksums
43b7b79ddf369c47711f1fa92445b23a73d96bfbf7c328dca519585b4e927bd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file

0.1.1

1 release file

0.1

1 release file

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