-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo service implemented in various frameworks
- Loading branch information
Showing
24 changed files
with
735 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# service_demo | ||
Simple examples for gRPC, Thrift and RPyC | ||
[](https://github.com/hardikp/service_demo/blob/master/LICENSE) | ||
|
||
Python3 server and client examples for [gRPC](https://grpc.io/), [Thrift](https://thrift.apache.org/docs/) and [RPyC](https://rpyc.readthedocs.io/en/latest/). | ||
|
||
## Service Example | ||
|
||
* `TimeService` implents `get_time` RPC call. | ||
* `get_time` returns the current server time in string format. | ||
|
||
## How to use | ||
|
||
1. Create an virtualenv and install requirements. | ||
```bash | ||
cd grpc | ||
pip3 install -r requirements.txt | ||
``` | ||
|
||
1. Run Server in one terminal window: | ||
```bash | ||
python3 server.py | ||
``` | ||
|
||
1. Run Client in another terminal: | ||
```bash | ||
python3 server.py |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import grpc | ||
|
||
import time_pb2 | ||
import time_pb2_grpc | ||
|
||
|
||
def run(): | ||
channel = grpc.insecure_channel('localhost:50051') | ||
stub = time_pb2_grpc.TimeStub(channel) | ||
response = stub.GetTime(time_pb2.TimeRequest()) | ||
print('Client received: {}'.format(response.message)) | ||
|
||
|
||
if __name__ == '__main__': | ||
run() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
grpcio | ||
grpcio-tools | ||
googleapis-common-protos |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import time | ||
from concurrent import futures | ||
|
||
import grpc | ||
|
||
import time_pb2 | ||
import time_pb2_grpc | ||
|
||
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 | ||
|
||
|
||
class Timer(time_pb2_grpc.TimeServicer): | ||
def GetTime(self, request, context): | ||
return time_pb2.TimeReply(message=time.ctime()) | ||
|
||
|
||
def serve(): | ||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) | ||
time_pb2_grpc.add_TimeServicer_to_server(Timer(), server) | ||
server.add_insecure_port('[::]:50051') | ||
server.start() | ||
try: | ||
while True: | ||
time.sleep(_ONE_DAY_IN_SECONDS) | ||
except KeyboardInterrupt: | ||
server.stop(0) | ||
|
||
|
||
if __name__ == '__main__': | ||
serve() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
syntax = "proto3"; | ||
package time; | ||
|
||
service Time { | ||
rpc GetTime (TimeRequest) returns (TimeReply) {} | ||
} | ||
|
||
// Empty Request Message | ||
message TimeRequest { | ||
} | ||
|
||
// The response message containing the time | ||
message TimeReply { | ||
string message = 1; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! | ||
import grpc | ||
|
||
import time_pb2 as time__pb2 | ||
|
||
|
||
class TimeStub(object): | ||
# missing associated documentation comment in .proto file | ||
pass | ||
|
||
def __init__(self, channel): | ||
"""Constructor. | ||
Args: | ||
channel: A grpc.Channel. | ||
""" | ||
self.GetTime = channel.unary_unary( | ||
'/time.Time/GetTime', | ||
request_serializer=time__pb2.TimeRequest.SerializeToString, | ||
response_deserializer=time__pb2.TimeReply.FromString, | ||
) | ||
|
||
|
||
class TimeServicer(object): | ||
# missing associated documentation comment in .proto file | ||
pass | ||
|
||
def GetTime(self, request, context): | ||
# missing associated documentation comment in .proto file | ||
pass | ||
context.set_code(grpc.StatusCode.UNIMPLEMENTED) | ||
context.set_details('Method not implemented!') | ||
raise NotImplementedError('Method not implemented!') | ||
|
||
|
||
def add_TimeServicer_to_server(servicer, server): | ||
rpc_method_handlers = { | ||
'GetTime': grpc.unary_unary_rpc_method_handler( | ||
servicer.GetTime, | ||
request_deserializer=time__pb2.TimeRequest.FromString, | ||
response_serializer=time__pb2.TimeReply.SerializeToString, | ||
), | ||
} | ||
generic_handler = grpc.method_handlers_generic_handler( | ||
'time.Time', rpc_method_handlers) | ||
server.add_generic_rpc_handlers((generic_handler,)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import rpyc | ||
|
||
conn = rpyc.connect('localhost', 18871) | ||
print('Time is {}'.format(conn.root.get_time())) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rpyc |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import time | ||
|
||
from rpyc import Service | ||
from rpyc.utils.server import ThreadedServer | ||
|
||
|
||
class TimeService(Service): | ||
def exposed_get_time(self): | ||
return time.ctime() | ||
|
||
|
||
if __name__ == '__main__': | ||
s = ThreadedServer(TimeService, port=18871) | ||
s.start() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python | ||
import sys | ||
|
||
from thrift import Thrift | ||
from thrift.protocol import TBinaryProtocol | ||
from thrift.transport import TSocket, TTransport | ||
sys.path.append('gen-py') | ||
from time_service import TimeService | ||
|
||
|
||
def main(): | ||
# Make socket | ||
transport = TSocket.TSocket('localhost', 9090) | ||
|
||
# Buffering is critical. Raw sockets are very slow | ||
transport = TTransport.TBufferedTransport(transport) | ||
|
||
# Wrap in a protocol | ||
protocol = TBinaryProtocol.TBinaryProtocol(transport) | ||
|
||
# Create a client to use the protocol encoder | ||
client = TimeService.Client(protocol) | ||
|
||
# Connect! | ||
transport.open() | ||
|
||
ts = client.get_time() | ||
print('Client Received {}'.format(ts)) | ||
|
||
# Close! | ||
transport.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main() | ||
except Thrift.TException as tx: | ||
print('%s' % tx.message) |
Empty file.
Oops, something went wrong.