Adds thrift client support to your Flask application
Project description
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:
unixs:/tmp/mysocket
tcps://localhost:5533/
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file Flask-ThriftClient-0.2.0.tar.gz.
File metadata
- Download URL: Flask-ThriftClient-0.2.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be0a1fee3c31a4579eebbefab6ac4bcd64f44090aa8e3bacd6c9d8735bbefa33
|
|
| MD5 |
2afbecf6463633de8cedf803948746cd
|
|
| BLAKE2b-256 |
43b7b79ddf369c47711f1fa92445b23a73d96bfbf7c328dca519585b4e927bd5
|