2017-01-24 3 views
1

내 프로토 파일입니다grpc 불법 선식 : 2 (예상 0)) 여기

syntax = "proto3"; 

package grpcClient; 

service GrpcClient { 
    rpc GetPeople(PeopleRequest) returns (PeopleResponse) {} 
} 
message PeopleRequest { 
    repeated string names = 1; // ex: ['jack', 'marie'] 
    repeated int32 ages = 2; // ex: [18, 24] 
} 
message PeopleResponse { 
    repeated Person people = 1; 
} 
message Person { 
    string name = 1; 
    int32 age = 2; 
} 

그리고 여기 파이썬 스텁 구조입니다 :

from . import grpcClient_pb2 
class GrpcClient: 
    def __init__(self): 
     server_url = "http://my_url:5001" 
     secure_channel = make_secure_channel(server_url) 
     self.__stub = grpcClient_pb2.GrpcClientStub(secure_channel) 

    def get_people(self, **kwargs): 
     people_request = grpcClient_pb2.PeopleRequest(**kwargs) 
     # your test example 
     try: 
      serialized_people_request = grpcClient_pb2.PeopleRequest.SerializeToString(people_request) 
     except Exception as e: 
      print(e) 
     else: 
      print('Serialized to binary of type {} and length {}'.format(type(serialized_people_request), len(serialized_people_request))) 
     return self.__stub.GetPeople(people_request) # this throw an error 

여기 내 grpc 전화입니다 :

grpcClient = GrpcClient() 
grpcClient.get_people({ 'names': ['daniel', 'jack'] }) # this is OK 
grpcClient.get_people({ 'names': ['daniel', 'jack'], 'ages': [18, 22] }) # this throws 

내가 사용 파이썬 3.6 내 핍 packa GES의 버전은 다음과 같습니다

python -m grpc.tools.protoc -I./ --python_out=./ --grpc_python_out=./ ./grpcClient.proto 

참고 :

grpcio   1.0.4  
grpcio-tools  1.0.4 
pip    9.0.1 
protobuf   3.1.0.post1 

내가이 명령을 사용하여 grpcClient_pb2.pygrpcClient_pb2_grpc.py 생성 그리고 여기입니다 자동 생성 된 파이썬 코드 :

_descriptor.FieldDescriptor(
     name='names', full_name='PeopleRequest.names', index=0, 
     number=1, type=9, cpp_type=9, label=3, 
     has_default_value=False, default_value=[], 
     message_type=None, enum_type=None, containing_type=None, 
     is_extension=False, extension_scope=None, 
     options=None), 
_descriptor.FieldDescriptor(
     name='ages', full_name='PeopleRequest.ages', index=1, 
     number=2, type=5, cpp_type=1, label=3, 
     has_default_value=False, default_value=[], 
     message_type=None, enum_type=None, containing_type=None, 
     is_extension=False, extension_scope=None, 
     options=None), 

많은 감사 당신이 도와 줘!

답변

0

짧은 이야기 : 내 서버는 Node.js를 작성하고 있었다 : rm -rf node_modules npm i 나를 위해 문제를 해결했다.

Long Story : 내 클라이언트가 연결 한 모든 경우 (내 로컬 및 원격 node.js 서버가 테스트를 위해 실행 중이 었음) 두 클라이언트에서 모두 던져 버릴 것입니다. 그래서 나는 서버 측이 아니라 클라이언트 코드에서 오류를 찾는다. 더욱이 서버는 아무 것도 기록하지 않았고 클라이언트 만 오류를 던졌습니다.

마지막으로 나는 파이썬에서 서버 측을 다시 구현했으며 놀랍게도 파이썬 서버에 연결할 때 내 클라이언트가 던지지 않을 것입니다. 그래서 나는 내 node.js 서버에 문제가 있다고 의심하기 시작했다. 마침내 완전히 내 node.js 서버의 node_modules을 다시 설치하고 다시 시작했다. 실제로 문제가 사라졌다.그래서 나는 문제가 어떤 패키지와 관련이 있다고 결론을 짓는다. node_modules.

1

문제를 진단하기에 충분한 정보가 없다고 생각합니다. 전체 proto 파일을 제공해 주시겠습니까? 나는 그것이 GetPeoples 서비스를 포함하고 있다고 가정하고 있습니다.

전체 클라이언트 프로그램도 제공 할 수 있습니까? gRPC를 사용하면 에서grpc 패키지와 생성 된 <your_proto>_pb2_grpc 패키지 모두를 가져와야하므로 가져 오기가 혼란 스러울 수 있습니다. 이 중 grpc.PeopleRequest이 무엇을 의미하는지는 명확하지 않습니다. 나를 위해 작동

syntax = "proto3"; 

service MyService { 
    rpc GetPeoples(PeopleRequest) returns (PeopleReply) {} 
} 

message PeopleRequest { 
    repeated string names = 1; // ex: ['jack', 'marie'] 
    repeated int32 ages = 2; // ex: [1, 2] 
} 

message PeopleReply { 
} 

그리고 다음 클라이언트 :

from __future__ import print_function 

import grpc 

import test_pb2 
import test_pb2_grpc 


def run(): 
    channel = grpc.insecure_channel('localhost:50051') 
    stub = test_pb2_grpc.MyServiceStub(channel) 
    obj = test_pb2.PeopleRequest(
     names=['daniel'], 
     ages=[32]) 
    print(obj) # Ok, no error when building the object 
    stub.GetPeoples(obj) # ERROR Illegal wire type for field ages: 2 (0 expected)) 

if __name__ == '__main__': 
    run() 

이이

은 내가 test.proto라는 이름의 다음과 같은 프로토 파일로 빠진 부분을 채우기 위해 노력했습니다

$ virtualenv venv 
$ source venv/bin/activate 
(venv) $ python --version 
Python 2.7.6 
(venv) $ pip install grpcio 
(venv) $ pip install cython 
(venv) $ pip install grpcio-tools 
(venv) $ pip freeze 
Cython==0.25.2 
argparse==1.2.1 
coverage==4.3.4 
enum34==1.1.6 
futures==3.0.5 
grpcio==1.0.4 
grpcio-tools==1.0.4 
protobuf==3.1.0.post1 
six==1.10.0 
wheel==0.29.0 
wsgiref==0.1.2 
(venv) $ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. test.proto 

행운을 빕니다 : 나는 per the Python quickstart를 구축 한 일 !

+0

테스트 해 주셔서 감사합니다. 이 티켓에 대한 자세한 내용은 https://github.com/grpc/grpc/issues/9439를 참조하십시오. 예제는 내 코드와 매우 유사합니다. 어쩌면 그것은 다른 python/grpc/protobuf 버전일까요? 지금 당장 키보드를 사용할 필요가 없습니다. 곧 더 조사하겠습니다. – Sulliwane

+0

(python/protobuf/grpcio)를 사용하고있는 버전을 알려주시겠습니까? test_pb2와 test_pb2_grpc는 어떻게 생성합니까? 감사합니다 – Sulliwane

+0

방금 ​​파이썬 3.5, 동일한 오류 던지고와 함께했습니다. 나는 당신의 수입에 대해 궁금합니다.'''test_pb2, import test_pb2_grpc''',''test_pb2_grpc'''는 문제를 일으키는 상단에 많은 중복 수입을 가지고 있습니다, 그래서 나는 그것을 건너 뛰고 사용합니다 스텁 정의는'''''test_pb2''''에서 가능합니다. – Sulliwane