最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python如何调用dubbo接口 python调用dubbo接口代码示例
时间:2020-09-24 编辑:袖梨 来源:一聚教程网
python如何调用dubbo接口?本篇文章小编给大家分享一下python调用dubbo接口代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
公司后端服务使用 java 重构后,很多接口采用了阿里的 dubbo 协议。而 python 是无法直接调用 dubbo 接口的,但可以通过 telnet 调用,具体可以通过 telnetlib 模块的 Telnet类 来调用,只需要四行代码即可实现:
import telnetlib # 创建telnet类对象 conn = telnetlib.Telnet() # 连接dubbo接口地址 conn.open(host, port) #1.cmd命令格式: 接口全名字.方法名(参数1,参数2,参数3...参数n) 2.write方法就是通过telnet发起dubbo请求,参数和单独使用telnet一致 conn.write('invoke {}n'.format(cmd).encode()) # 获取telnet返回信息 conn.read_until('dubbo>'.encode()).decode().split('rn')[0]
分装成类:
class Dubbo(object): ''' 方法调用案例: conn = Dubbo('127.0.0.1', 18080) #格式: 接口全名字.方法名(参数1,参数2,参数3...参数n) cmd = 'xxx.xxx.xx.xxxx.xxxx.xxxx.xxxx(268,"sz",1587288615000,1587634215000,0,10)' response = json.loads(conn.reuqest(cmd)) ''' dubbo = 'dubbo>' def __init__(self,host,port): self.conn = telnetlib.Telnet() self.conn.open(host, port) def request(self,cmd): self.conn.write('invoke {}n'.format(cmd).encode()) data = self.conn.read_until(self.dubbo.encode()).decode().split('rn')[0] return data
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27