main question: in python Implement the simplest client-server gRPC application "HelloWorld", which consists in sending a message to a remote server and receiving a response from it. *I will try to explain much I can because each time run it it doesn't work. please I do my best to explain the task and provide the code please help me do it for me cause I stuck in, I separate each code to be clear: Let's consider the simplest example of realization of the application using remote method invocation on GRPC technology. In the PyCharm environment, create a new project named GRPC with program packages written in Python. Open a terminal window and type the following commands python -m pip install --upgrade pip python -m pip install grpcio python -m pip install grpcio-tools after: The gRPC protocol, like many other RPCs, is based on the idea of describing services and methods that can be invoked remotely. By default, gRPC uses Protocol Buffer to describe services and message structure. For .proto files we will create a separate directory in our project structure with the name "protos": In this directory, create a file called "helloworld.proto" in the file helloword.proto inside the directory protos we will run this: -------------------------------------------------------------------------------------------------------------------------------- syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; string surname = 2; } message HelloReply { string message = 1; } --------------------------------------------------------------------------------------------------------------- After that, we need to compile this file by typing the command in the terminal: python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. protos/helloworld.proto Create 2 new python files named "Server.py" and "Client.py" At this point, your project should contain the following files: Files с names "helloworld_pb2" & "helloworld_pb2_grpc" are generated automatically when "helloworld.proto" is compiled Realize the source code of the server.py: -------------------------------------------------------------------------------------------------------------------------------------- from concurrent import futures import time import grpc import helloworld_pb2 import helloworld_pb2_grpc _ONE_DAY_IN_SECONDS = 60 * 60 * 24 class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): return helloworld_pb2.HelloReply(message='Hello, %s %s!' % (request.name, request.surname)) def serve(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), 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__': greeter_instance = Greeter() greeter_instance.serve() --------------------------------------------------------------------------------------------------------------------------- Implement the client.py source code: ------------------------------------------------------------------------------------------------------------------------ import grpc import helloworld_pb2 import helloworld_pb2_grpc def run(): channel = grpc.insecure_channel('localhost:50051') stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello(helloworld_pb2.HelloRequest(name='dear', surname='friend')) print("Greeter client received: \n" + response.message + '\n') if __name__ == '__main__': run() ---------------------------------------------------------------------------------------------------------------- After that, we can call it in: response = stub.SayHello(helloworld_pb2.HelloRequest(name='dear', surname='friend'))

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

 

main question: in python Implement the simplest client-server gRPC application "HelloWorld", which consists in sending a message to a remote server and receiving a response from it.

*I will try to explain much I can because each time run it it doesn't work. please I do my best to explain the task and provide the code  please help me do it for me cause I stuck in, I separate each code to be clear:

Let's consider the simplest example of realization of the application using remote method invocation
on GRPC technology.
In the PyCharm environment, create a new project named GRPC

with program packages written in Python. Open a terminal window and type the following commands

python -m pip install --upgrade pip
python -m pip install grpcio
python -m pip install grpcio-tools

after: The gRPC protocol, like many other RPCs, is based on the idea of describing services and
methods that can be invoked remotely. By default, gRPC uses Protocol Buffer to describe
services and message structure. For .proto files we will create a separate directory in our project
structure with the name "protos":

In this directory, create a file called "helloworld.proto"

in the file helloword.proto inside the directory protos we will run this:

--------------------------------------------------------------------------------------------------------------------------------

syntax = "proto3";
package helloworld;

service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
    string name = 1;
    string surname = 2;
}

message HelloReply {
    string message = 1;
}

---------------------------------------------------------------------------------------------------------------

After that, we need to compile this file by typing the command in the terminal: python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. protos/helloworld.proto

Create 2 new python files named "Server.py" and "Client.py" At this
point, your project should contain the following files: Files с names "helloworld_pb2" & "helloworld_pb2_grpc" are generated
automatically when "helloworld.proto" is compiled

Realize the source code of the server.py: 

--------------------------------------------------------------------------------------------------------------------------------------

from concurrent import futures
import time
import grpc
import helloworld_pb2
import helloworld_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s %s!' % (request.name, request.surname))

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), 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__':
    greeter_instance = Greeter()
    greeter_instance.serve()

---------------------------------------------------------------------------------------------------------------------------

Implement the client.py source code:

------------------------------------------------------------------------------------------------------------------------

import grpc
import helloworld_pb2
import helloworld_pb2_grpc

def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = helloworld_pb2_grpc.GreeterStub(channel)
    response = stub.SayHello(helloworld_pb2.HelloRequest(name='dear', surname='friend'))
    print("Greeter client received: \n" + response.message + '\n')

if __name__ == '__main__':
    run()

----------------------------------------------------------------------------------------------------------------

After that, we can call it in:

response = stub.SayHello(helloworld_pb2.HelloRequest(name='dear',
surname='friend'))

1: Project
GRPC
1: Project
Project
GRPC PR
Extema
Scratch 6 Cut
New
Copy
Copy Path
Copy Relative Path
Easte
Find Usages
Find in Path...
Replace in Path...
Inspect Code...
Refactor
Clean Python Compiled Files
GRPC) protos
Project
IGRPC D:\GRPC
protos
> External Libraries
Ctrl+X
Ctrl+C
Ctrl+Shift+C
Ctrl+Alt-Shift+C
Scratches and Console
Ctrl+V
Alt+F7
Ctrl+Shift+F
Ctrl+Shift+R
>
File
New Scratch File
In this directory, create a file called "helloworld.proto":
Directory
Python Package
Python File
Jupyter Notebook
HTML File
Resource Bundle
New File
Enter a new file name:
helloworld.proto
OK
Ctrl+Alt+Shift+Insert
Cancel
X
Transcribed Image Text:1: Project GRPC 1: Project Project GRPC PR Extema Scratch 6 Cut New Copy Copy Path Copy Relative Path Easte Find Usages Find in Path... Replace in Path... Inspect Code... Refactor Clean Python Compiled Files GRPC) protos Project IGRPC D:\GRPC protos > External Libraries Ctrl+X Ctrl+C Ctrl+Shift+C Ctrl+Alt-Shift+C Scratches and Console Ctrl+V Alt+F7 Ctrl+Shift+F Ctrl+Shift+R > File New Scratch File In this directory, create a file called "helloworld.proto": Directory Python Package Python File Jupyter Notebook HTML File Resource Bundle New File Enter a new file name: helloworld.proto OK Ctrl+Alt+Shift+Insert Cancel X
Create 2 new python files named "Server.py" and "Client.py" At this
point, your project should contain the following files:
1: Project
gRPCLab protos
Project
IgRPCLab D:\gRPCLab
protos
Client.py
helloworld_pb2.py
helloworld_pb2_grpc.py
Server.py
IllExternal Libraries
Scratches and Consoles
Transcribed Image Text:Create 2 new python files named "Server.py" and "Client.py" At this point, your project should contain the following files: 1: Project gRPCLab protos Project IgRPCLab D:\gRPCLab protos Client.py helloworld_pb2.py helloworld_pb2_grpc.py Server.py IllExternal Libraries Scratches and Consoles
Expert Solution
Step 1: Python Program:

Answer:

The given python files and the steps you followed are seems to be correct, but there was a small problem regarding to the server.py program. Let us resolve that.

The serve() function should be called at the end, not the greeter_instance.serve().

server.py:



from concurrent import futures
import time
import grpc
import helloworld_pb2
import helloworld_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s %s!' % (request.name, request.surname))

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), 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()   #modification

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Introduction to Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education