Skip to main content

gRPC for Django.

Project description

https://img.shields.io/pypi/v/djangogrpcframework.svg https://readthedocs.org/projects/djangogrpcframework/badge/?version=latest https://travis-ci.org/fengsp/django-grpc-framework.svg?branch=master https://img.shields.io/pypi/pyversions/djangogrpcframework https://img.shields.io/pypi/l/djangogrpcframework

Django gRPC framework is a toolkit for building gRPC services, inspired by djangorestframework.

Installation

$ pip install djangogrpcframework

Add django_grpc_framework to INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'django_grpc_framework',
]

Demo

Here is a quick example of using gRPC framework to build a simple model-backed service for accessing users, startup a new project:

$ django-admin startproject demo
$ python manage.py migrate

Now define protos in demo.proto:

syntax = "proto3";

package demo;

import "google/protobuf/empty.proto";

message User {
    int32 id = 1;
    string username = 2;
    string email = 3;
}

service UserController {
    rpc List(google.protobuf.Empty) returns (stream User) {}
    rpc Create(User) returns (User) {}
    rpc Retrieve(User) returns (User) {}
    rpc Update(User) returns (User) {}
    rpc Destroy(User) returns (google.protobuf.Empty) {}
}

Generate gRPC code:

python -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ ./demo.proto

Now edit the demo/urls.py module:

from django.contrib.auth.models import User
from rest_framework import serializers
from django_grpc_framework import generics
import demo_pb2
import demo_pb2_grpc


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email']


class UserService(generics.ModelService):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    protobuf_class = demo_pb2.User


urlpatterns = []
def grpc_handlers(server):
    demo_pb2_grpc.add_UserControllerServicer_to_server(UserService.as_servicer(), server)

That’s it, we’re done!

$ python manage.py grpcrunserver --dev

You can now run a gRPC client to access the service:

from google.protobuf import empty_pb2

with grpc.insecure_channel('localhost:50051') as channel:
    stub = demo_pb2_grpc.UserControllerStub(channel)
    for user in stub.List(empty_pb2.Empty()):
        print(user, end='')

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

djangogrpcframework-0.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangogrpcframework-0.1-py2.py3-none-any.whl (15.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file djangogrpcframework-0.1.tar.gz.

File metadata

  • Download URL: djangogrpcframework-0.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for djangogrpcframework-0.1.tar.gz
Algorithm Hash digest
SHA256 5a11af014ab0e9d840e8e84ab3b2df3c140f74d757c10af1a2b24e516a338842
MD5 15f9f4d0d1766b3c277aced6f480051a
BLAKE2b-256 998abd703adb86764269336d0c9b4c1b777e38da9fd17397bb85f9f5a9326dcc

See more details on using hashes here.

File details

Details for the file djangogrpcframework-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for djangogrpcframework-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 11d58f74dbd2a5d9797b77fb850e6f8a02a7a5c3248614ea16dee8b3f040de1b
MD5 6da48e9a79a87ca313ad97fc6f994eda
BLAKE2b-256 932681f86038a3d24878217168a5c897b05b0bf573df680fcc679f14a04f2bb9

See more details on using hashes here.

Supported by

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