Skip to main content

gRPC-Web for Python

Project description

CircleCI

Sonora

Sonora is a Python-first implementation of gRPC-Web built on top of standard Python APIs like WSGI and ASGI for easy integration.

Why?

Regular gRPC has a lot going for it but is awkward to use in some environments. gRPC-Web makes it easy to get gRPC working in environments that need HTTP/1.1 but the Google gRPC and gRPC-Web implementations don't like to coexist with your normal Python frameworks like Django or Flask. Unlike grpc/grpc Sonora doesn't care what ioloop you use, this means you can run it along side any other Python web framework in the same application!

This makes it easy to

  • Add gRPC to an existing code base.
  • Run gRPC behind AWS and other HTTP/1.1 load balancers.
  • Integrate with other ASGI frameworks like Channels, Starlette, Quart etc.
  • Integrate with other WSGI frameworks like Flask, Django etc.

The name Sonora was inspired by the Sonoran gopher snake.

Snek

How?

Sonora is designed to require minimal changes to an existing Python application.

Server

WSGI

Normally a WSGI application (such as your favourite Django app) will have a file somewhere named wsgi.py that gets your application setup and ready for your web server of choice.

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

in it somewhere so that your application server (uWSGI, Gunicorn etc) knows where your code is.

To add Sonora's gRPC-Web capabilities to an application like the above all you need to do to enable it is this.

from django.core.wsgi import get_wsgi_application
from sonora.wsgi import grpcWSGI
import helloworld_pb2_grpc

# Setup your frameworks default WSGI app.

application = get_wsgi_application()

# Install the Sonora grpcWSGI middleware so we can handle requests to gRPC's paths.

application = grpcWSGI(application)

# Attach your gRPC server implementation.

helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), application)

And now you have a combined HTTP/1.1 Django + gRPC application all under a single port.

ASGI

For ASGI things are mostly the same, the example shown here integrates with Quart but it's more or less the same for other frameworks.

from sonora.asgi import grpcASGI
from quart import Quart

# Setup your frameworks default ASGI app.

application = Quart(__name__)

# Install the Sonora grpcASGI middleware so we can handle requests to gRPC's paths.

application = grpcASGI(application)

# Attach your gRPC server implementation.

helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), application)

And now you have a combined HTTP/1.1 Quart + gRPC application all under a single port.

Clients

Sonora currently only provides a sync API implementation based on requests.

Requests (Sync)

Instead of using gRPCs native insecure_channel API we have sonora.client.insecure_web_channel instead which provides a requests powered client channel to a gRPC-Web server. e.g.

    import sonora.client

    with sonora.client.insecure_web_channel(
        f"http://localhost:8080"
    ) as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        print(stub.SayHello("world"))

Aiohttp (Async)

Pull requests welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sonora-0.0.5.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

sonora-0.0.5-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file sonora-0.0.5.tar.gz.

File metadata

  • Download URL: sonora-0.0.5.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.8.0 tqdm/4.40.0 CPython/3.7.4

File hashes

Hashes for sonora-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e94aeefeb9628f5b917bda2d3f93ed265d35e3e952ef059f335e466c643533a9
MD5 084296751833c77b3459209777865c87
BLAKE2b-256 bc5b47ca4967341130ea195e1be5df94b7e9841dfc334927f5b2c4a5c6d1df62

See more details on using hashes here.

Provenance

File details

Details for the file sonora-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: sonora-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.8.0 tqdm/4.40.0 CPython/3.7.4

File hashes

Hashes for sonora-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b97f6f871e030200f621fd356b26d554b10ce40fc2201a99d19c64a6e2355a91
MD5 1dcf67b248737ec88f9ef8adeddc7470
BLAKE2b-256 dc961529cefd2e7612a229045be1cab18b096cce40ec05d99877a55877d6a42d

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page