原文地址:https://hhyo.github.io/2017/10/20/python-dubbo-telnet/
最近在测试项目中接触到dubbo框架,由于业务逻辑复杂,前台一个业务流程在后端会依赖多个服务提供数据,而各方开发进度也不完全一致。在业务测试脚本编写完成后,希望能够在项目整体提测前,先验证部分已提供的dubbo接口的可用性。另外一方面,也能够在测试过程中更快的定位到具体的服务提供者,指派任务,减少反复沟通定位问题所耗费的时间。
先说环境:
OS: macOS High Sierra
python: 2.7
步骤:
- 准备python环境,pip安装dubbo_telnet:
pip install dubbo_telnet
- 编写如下调试脚本
1234567891011121314151617181920212223242526272829 |
#-*- coding: utf-8 -*- import dubbo_telnetimport jsonHost = ‘192.168.0.1‘ # Doubble服务器IPPort = 9036 # Doubble服务端口 # 初始化dubbo对象conn = dubbo_telnet.connect(Host, Port) # 设置telnet连接超时时间conn.set_connect_timeout(10) # 设置dubbo服务返回响应的编码conn.set_encoding(‘gbk‘) # 显示服务列表print conn.do("ls") # 显示指定服务的方法列表print conn.do("ls XXXService") # 方法调用interface = ‘XXXService‘method = ‘userinfo‘param = user_idresult = conn.invoke(interface, method, param) print json.dumps(result, sort_keys=True, indent=4, separators=(‘,‘, ‘: ‘), skipkeys=True, ensure_ascii=False) |
这时候运行脚本,抛错
123 |
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded")ValueError: No JSON object could be decoded |
检查site-packages/dubbo_telnet/init.py文件
1234567 |
data = ‘‘while data.find(self.__finish) == -1: data = tn.read_very_eager()data = data.split("\n")data = json.loads(data[0], encoding=self.__encoding)tn.close() # tn.write(‘exit\n‘)return data |
可以知道是在对返回结果处理时出了异常,无法转换为python对象,在这里加上一个错误处理
12345 |
try: data = json.loads(data[0], encoding=self.__encoding)except Exception as ValueError: data = data#data = json.loads(data[0], encoding=self.__encoding) |
就可以正常返回结果,可以进行简单的dubbo请求和查看响应了。其实整个过程就是利用telnet命令来连接和操作dubbo服务,也可以直接在命令行中进行。
附上dubbo的telnet命令:
12345678910111213 |
Please input "help [command]" show detail. status [-l] - Show status. pwd - Print working default service. trace [service] [method] [times] - Trace the service. exit - Exit the telnet. help [command] - Show help. invoke [service.]method(args) - Invoke the service method. count [service] [method] [times] - Count the service. clear [lines] - Clear screen. ls [-l] [service] - List services and methods. log level - Change log level or show log ps [-l] [port] - Print server ports and connections. cd [service] - Change default service. |
原文地址:https://www.cnblogs.com/slqt/p/11346923.html
时间: 2024-10-11 07:26:42