原文地址: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
- 编写如下调试脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
|
这时候运行脚本,抛错
1 2 3
|
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文件
1 2 3 4 5 6 7
|
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()
|
可以知道是在对返回结果处理时出了异常,无法转换为python对象,在这里加上一个错误处理
1 2 3 4 5
|
try: data = json.loads(data[0], encoding=self.__encoding) except Exception as ValueError: data = data
|
就可以正常返回结果,可以进行简单的dubbo请求和查看响应了。其实整个过程就是利用telnet命令来连接和操作dubbo服务,也可以直接在命令行中进行。
附上dubbo的telnet命令:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
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.
|