头条项目 8.6 编写客户端"/>
黑马头条项目 8.6 编写客户端
在toutiao-backend/common/rpc目录下新建client.py
import grpc
import reco_pb2
import reco_pb2_grpc
import timedef feed_articles(stub):# 构建rpc调用的调用参数user_request = reco_pb2.UserRequest()user_request.user_id = '1'user_request.channel_id = 1user_request.article_num = 10user_request.time_stamp = round(time.time()*1000)# 通过stub进行方法调用,并接收调用返回值ret = stub.user_recommend(user_request)print('ret={}'.format(ret))def run():"""构建连接rpc服务器的对象rpc客户端调用的方法"""# 使用with语句连接rpc服务器with grpc.insecure_channel('127.0.0.1:8888') as channel:# 创建调用rpc远端服务的辅助对象stubstub = reco_pb2_grpc.UserRecommendStub(channel)# 可以通过stub进行rpc调用# ret=stub.user_recommend()# 通过stub进行rpc调用feed_articles(stub)if __name__ == '__main__':run()
更多推荐
黑马头条项目 8.6 编写客户端
发布评论