a2s_client.py 944 B

123456789101112131415161718192021222324252627282930
  1. # coding:utf-8
  2. import grpc
  3. from proto import service_pb2, service_pb2_grpc
  4. import logging
  5. def a2s_execute(a2s_ip: str, topic: str, timeout: int, bytes_data: bytes):
  6. """
  7. a2e服务调用
  8. :param a2s_ip: 192.168.3.240:9090 # 服务地址
  9. :param topic: t2v 主体
  10. :param timeout: 60 超时时间
  11. :param bytes_data: 请求字段
  12. :return: bytes or None
  13. """
  14. try:
  15. if not (a2s_ip and topic):
  16. raise IOError("参数错误:a2s_ip和topic不能为空!")
  17. with grpc.insecure_channel(a2s_ip) as channel:
  18. # 客户端实例
  19. stub = service_pb2_grpc.CallerStub(channel)
  20. # 调用服务端方法
  21. response = stub.Call(service_pb2.Request(topic=topic, timeout=timeout, data=bytes_data))
  22. # 获取结果
  23. data = response.data
  24. return data
  25. except Exception as e:
  26. logging.warning(e)
  27. return None