如何使用grpc在python服务器和php客户端之间进行通信?

罗希特·库玛(Rohit Kumar)

我可以使用python客户端和服务器进行通信,但与php客户端混淆。我对probubufer感到困惑,一个解释整个过程的小程序会很有帮助。

我浏览了许多文档,但对实际流程仍然很困惑。

计算器

syntax = "proto3";

message Request {
    int32 num1 = 1;
    int32 num2 = 2;
}

message Response{
    int32 result = 1;
}

service Calculator {
    rpc Sum(Request) returns (Response) {}
}

计算器

def sum(x1,x2):
  y= x1+x2
  return y

server.py

import grpc
from concurrent import futures
import time

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# import the original calculator.py
import calculator

# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):

    # calculator.sum is exposed here
    def Sum(self, request, context):
        response = calculator_pb2.Response()
        response.result = calculator.sum(request.num1,request.num2)
        print 'Result:',response.result
        return response


# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
calculator_pb2_grpc.add_CalculatorServicer_to_server(
        CalculatorServicer(), server)

# listen on port 50051
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

client.py

import grpc

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')

# create a stub (client)
stub = calculator_pb2_grpc.CalculatorStub(channel)
while True:
    try:
        # create a valid request message
        numbers = calculator_pb2.Request(num1=int(input("Enter number1: ")),num2=int(input("Enter number2: ")))
    # make the call
        response = stub.Sum(numbers)

        # print 'Result:',response.result
    except KeyboardInterrupt:
        print("KeyboardInterrupt")
        channel.unsubscribe(close)
        exit()

此设置返回服务器(python)中两个数字的加法。我希望使用与python作为服务器和php作为客户端相同的功能。

罗希特·库玛(Rohit Kumar)

我可以与php客户端和python服务器通信。以下是步骤和示例:

脚步:


--------------------------------
           Python
--------------------------------

1. Make a folder named grpc

2. Install pip for python

3.Install virtual environment
  python -m pip install virtualenv

4. Run the commands
   virtualenv venv
   source venv/bin/activate
   python -m pip install --upgrade pip

5. Then install grpcio
   python -m pip install grpcio

6. Install grpc tools
   python -m pip install grpcio-tools

7. Generate protobuf
   python -m grpc_tools.protoc -I./proto --python_out=. --grpc_python_out=. ./proto/calculator.proto 

8. Make server.py


---------------------------------
           PHP
---------------------------------

1. Install composer
   composer install

2. Run these commands to install grpc using pecl
   sudo apt-get install php-dev
   pecl
   sudo pecl install grpc

3. Download protoc version > 3.7
commands:
PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
curl -OL   https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP  

4. Clone this repo
   git clone -b v1.27.0 https://github.com/grpc/grpc


5. Run this command
   cd grpc && git submodule update --init && make grpc_php_plugin
   cd examples/php/route_guide
   ./route_guide_proto_gen.sh

6. Move to root then run: make grpc_php_plugin

7.Generate protobuf
   protoc --proto_path=examples/protos --php_out=examples/php/route_guide --grpc_out=examples/php/route_guide --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin ./examples/protos/calculator.proto

8. Make index.php

index.php


<?php

include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/CalculatorClient.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/SumRequest.php';
include __DIR__ . '/grpc/examples/php/route_guide/Calculator/SumResponse.php';
include __DIR__ . '/grpc/examples/php/route_guide/GPBMetadata/Calculator.php';

function add()
{
    // Listening to port
    $client = new \Calculator\CalculatorClient('localhost:6000', [
        'credentials' => Grpc\ChannelCredentials::createInsecure(),
    ]);
    while (true) {
        try {
            // request object
            $request = new \Calculator\SumRequest();
            echo " Num1: ";
            // num1
            $request->setNum1((int) rtrim(fgets(STDIN)));
            echo " Num2: ";
            // num2
            $request->setNum2((int) rtrim(fgets(STDIN)));

            list($res, $status) = $client->Sum($request)->wait();
            // result
            echo $res->getResult() . "\n";

        } catch (Exception $error) {
            echo $error;
        }
    }
}

try {
    add();
    //    phpinfo();
} catch (Exception $e) {
    echo $e;
}

计算器


def sum(x1,x2):
  y= x1+x2
  return y

server.py

import grpc
from concurrent import futures
import time

# import the generated classes
import calculator_pb2
import calculator_pb2_grpc

# import the original calculator.py
import calculator

# create a class to define the server functions, derived from
# calculator_pb2_grpc.CalculatorServicer
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):

    # calculator.sum is exposed here
    def Sum(self, request, context):
        response = calculator_pb2.SumResponse()
        response.result = calculator.sum(request.num1,request.num2)
        print 'Result:',response.result
        return response


# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

# use the generated function `add_CalculatorServicer_to_server`
# to add the defined class to the server
calculator_pb2_grpc.add_CalculatorServicer_to_server(
        CalculatorServicer(), server)

# listen on port 50051
print('Starting server. Listening on port 6000.')
server.add_insecure_port('[::]:6000')
server.start()

# since server.start() will not block,
# a sleep-loop is added to keep alive
try:
    while True:
        time.sleep(86400)
except KeyboardInterrupt:
    server.stop(0)

计算器



syntax = "proto3";

package Calculator;
message SumRequest {
    int64 num1 = 1;
    int64 num2 = 2;
}

message SumResponse{
    int64 result = 1;
}

service Calculator {
    rpc Sum(SumRequest) returns (SumResponse) {}
}

还可以通过以下链接获得更好的理解:PHP- https : //grpc.io/docs/tutorials/basic/php/ Python- https://grpc.io/docs/tutorials/basic/python/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

服务器与客户端之间的通信标准

在Spring框架中,如何在服务器和客户端之间进行快速通信?

使用网络服务发现在Java服务器和Android客户端之间进行通信

无法使用SSL在Java客户端和python服务器之间执行通信

使用javascript在服务器和客户端之间进行XML通信

使用UDP在服务器和客户端之间进行文件传输(.png,.txt)

如何使用套接字IO在Python客户端和NodeJS服务器之间进行通信?

python套接字,尝试在服务器和客户端之间进行交互登录

服务器与客户端之间的通信执行的操作

如何使用Prometheus监视grpc-java服务器和客户端?

如何在Java服务器和JavaScript客户端之间使用套接字通信?

如何在ASP.NET Web应用程序中的服务器和客户端之间进行通信?

在grpc客户端之间传输“斑点”-服务器

服务器与客户端之间的通信失败

服务器和客户端之间以两种方式进行进程间通信的最佳方式

服务器和客户端之间的实时通信

如何在PHP中作为客户端与CGI服务器进行通信?

为什么DHCP使用UDP端口67和68进行客户端和服务器之间的通信?

在Java服务器和PHP客户端之间进行通信

GWT-(客户端->服务器->客户端)和(服务器->客户端)通信之间的区别

如何使用PHP和Apache Web服务器正确调用Python Pyro客户端?

如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?

Python 服务器和客户端无法通信

服务器和客户端之间的持续通信

如何使用 python 与 MySQL 服务器上的其他客户端连接进行通信?

如何解决“服务器和客户端之间没有通信”

TCP 套接字客户端和 protobuf 可以与 gRPC 服务器通信吗?

如何在服务器和客户端之间保持 grpc 服务定义同步?

使用 gRPC,我应该如何处理服务器和客户端之间的部分结果?