This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""etcd3 Leader election.""" | |
import sys | |
import time | |
from threading import Event | |
import etcd3 | |
LEADER_KEY = '/leader' | |
LEASE_TTL = 5 | |
SLEEP = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read in key and certificate | |
with open('server.key', 'rb') as f: | |
private_key = f.read() | |
with open('server.crt', 'rb') as f: | |
certificate_chain = f.read() | |
# create server credentials | |
server_credentials = grpc.ssl_server_credentials( | |
((private_key, certificate_chain,),)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# read in certificate | |
with open('server.crt', 'rb') as f: | |
trusted_certs = f.read() | |
# create credentials | |
credentials = grpc.ssl_channel_credentials(root_certificates=trusted_certs) | |
# create channel using ssl credentials | |
channel = grpc.secure_channel('{}:{}'.format(host, port), credentials) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grpc | |
from grpc._cython.cygrpc import CompressionAlgorithm | |
from grpc._cython.cygrpc import CompressionLevel | |
chan_ops = [('grpc.default_compression_algorithm', CompressionAlgorithm.gzip), | |
('grpc.grpc.default_compression_level', CompressionLevel.high)] | |
chan = grpc.insecure_channel("<srv_addr>:<srv_port>", chan_ops) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ServerServicer(service_pb2_grpc.ServerServicer): | |
def Foo(self, request, context): | |
metadata = context.invocation_metadata() | |
print 'metadata: {}'.format(metadata) | |
return service_pb2.Empty() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stub = service_pb2_grpc.ServerStub(channel) | |
stub.Foo(service_pb2.Empty(), metadata=[('foo', 'bar')]) |