用Python进行gRPC接口测试(二)_python 如何执行 grpc 接口测试(2)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

Point lo = 1;
  Point hi = 2;
}
message Feature {
  string name = 1;
  Point location = 2;
}

message RouteNote {
  Point location = 1;
  string message = 2;
}

message RouteSummary {
  int32 point_count = 1;
  int32 feature_count = 2;
  int32 distance = 3;
  int32 elapsed_time = 4;
}


根据协议文件生成route\_guide\_pb2.py、route\_guide\_pb2\_grpc.py两个必要的模块文件,然后就可以根据他们来创建客户端了。


## 二、客户端实现


##### 1、应答流式RPC


应答流式RPC返回的内容为流式,一次请求,n次返回。我们可以用for循环来接收返回的内容:



def guide_list_features(stub):
    rectangle = route_guide_pb2.Rectangle(
        lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
        hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
    print(“Looking for features between 40, -75 and 42, -73”)

features = stub.ListFeatures(rectangle)

for feature in features:
        print(“Feature called %s at %s” % (feature.name, feature.location))


##### 2、请求流式RPC


请求流式RPC请求的内容为流式,n次请求,一次返回。我们可以用迭代器来发送若干份请求数据:



def generate_route(feature_list):
    for _ in range(0, 10):
        random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
        print(“Visiting point %s” % random_feature.location)
        yield random_feature.location

def guide_record_route(stub):
    feature_list = route_guide_resources.read_route_guide_database()

route_iterator = generate_route(feature_list)
    route_summary = stub.RecordRoute(route_iterator)
    print("Finished trip with %s points " % route_summary.point_count)
    print("Passed %s features " % route_summary.feature_count)
    print("Travelled %s meters " % route_summary.distance)
    print("It took %s seconds " % route_summary.elapsed_time)


其中route\_iterator为一个迭代器。


##### 3、双向流式RPC


双向流式RPC请求的内容为流式,返回内容也为流式,n次请求,n次返回。我们可以用迭代器来发送若干份请求数据,通过for循环来接收返回结果:



def generate_messages():
    messages = [
        make_route_note(“First message”, 0, 0),
        make_route_note(“Second message”, 0, 1),
        make_route_note(“Third message”, 1, 0),
        make_route_note(“Fourth message”, 0, 0),
        make_route_note(“Fifth message”, 1, 0),
    ]
    for msg in messages:
        print(“Sending %s at %s” % (msg.message, msg.location))
        yield msg

def guide_route_chat(stub):
    responses = stub.RouteChat(generate_messages())
    for response in responses:
        print(“Received message %s at %s” %
              (response.message, response.location))


## 三、实际应用


在录音笔项目中,需要对转写后的文本进行分段语义整理,由于文本内容可能较多,服务端需要采用流式的方式进行接收,并通过流式的方式将结果返给客户端,于是这里采用了双向流式RPC形式的接口。


接口协议如下(仅为演示需要,只展示部分内容):



syntax = “proto3”;

package sogou.parrot.inner.semantic.v1;
import “google/protobuf/duration.proto”;
import “record.proto”;
option go_package = “git.speech.sogou/semantic/v1;semantic”;

service discourse_understand{
    rpc UnderstandFullText(stream UnderstandFullTextRequest) returns(stream UnderstandFullTextResponse);        
}

message UnderstandFullTextRequest{
    repeated SubSentence sub_sentences = 1;
    repeated sogou.parrot.record.v1.NonSpeechSoundInfo sound_infos = 2;
    repeated sogou.parrot.record.v1.AIMark ai_marks = 3;
}

message UnderstandFullTextResponse{
        UnderstandFullTextResult result = 2;
}


实现客户端的关键代码如下:



def gen_iterator(request):
    for r in [request]:
        yield r

def get_understand_full_textresponse(stub, ai_marks, sound_infos, sub_sentences):
    request = UnderstandFullTextRequest()
    request.sub_sentences.extend(sub_sentences)
    request.sound_infos.extend(sound_infos)
    request.ai_marks.extend(ai_marks)
    request_iter = gen_iterator(request)
    try:
        resps = stub.UnderstandFullText(request_iter)
        for resp in resps:
            resp_str = json.dumps(json.loads(MessageToJson(resp)),indent=4, ensure_ascii=False)
            print(resp_str)
    except Exception as e:
        print (e)

def run():
    ai_marks, sound_infos, sub_sentences = extract_data()
    with grpc.insecure_channel(sys.argv[2]) as channel:
        stub = discourse_understandStub(channel)
        print(“-------------- UnderstandFullText --------------”)
        get_understand_full_textresponse(stub, ai_marks, sound_infos, sub_sentences)

if name == ‘main’:
    run()


运行客户端,可以成功返回结果:


![](https://img-blog.csdnimg.cn/c783fadb400d4a198974d89f0bbd294e.png)




![img](https://img-blog.csdnimg.cn/img_convert/8266418a8cd0bea38b76beed6b60c618.png)
![img](https://img-blog.csdnimg.cn/img_convert/8ef53afcd98f8f4b957b30661a1f1e66.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 13
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值