import json
import socket
import telnetlib
class Dubbo(telnetlib.Telnet):
prompt = 'dubbo>'
coding = 'utf-8'
def __init__(self, host=None, port=0,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
super().__init__(host, port)
self.write(b'\n')
def command(self, flag, str_=""):
data = self.read_until(flag.encode())
self.write(str_.encode() + b"\n")
return data
def invoke(self, service_name, method_name, arg):
command_str = "invoke {0}.{1}({2})".format(
service_name, method_name, json.dumps(arg))
self.command(Dubbo.prompt, command_str)
data = self.command(Dubbo.prompt, "")
data = data.decode(Dubbo.coding, errors='ignore').split('\n')[1].strip()
return data
def do(self, arg):
command_str = arg
self.command(Dubbo.prompt, command_str)
data = self.command(Dubbo.prompt, command_str)
data = data.decode(Dubbo.coding, errors='ignore').split('\n')[1].strip()
return data
if __name__ == '__main__':
conn = Dubbo('127.0.0.1', 20881)
# 格式invoke 接口全名字.方法名(参数1,参数2,参数3...参数n)
command_str1 = 'invoke com.ecarx.ai.commonapi.MatchFlightPartternService.matchfllight("1",1)'
data1 = {
"class": "com.xxx.ai.commonapi.entity.User",
"userid": 2016020201809846,
"name": "好久好久",
"adr": {
"class": "com.xxx.ai.commonapi.entity.Adress",
"homeadress": "123",
"companyadress": "456"
}
}
data2 ={
"class": "com.xxx.ai.commonapi.entity.User",
"userid": 2016020201809846,
"name": "好久好久",
"adr": {
"class": "com.xxx.ai.commonapi.entity.Adress",
"homeadress": "123",
"companyadress": "456"
},
"child": {
"son": "haha",
"daughter": "xixi"
}
}
data3 ={
"class": "com.xxx.ai.commonapi.entity.User",
"userid": 2016020201809846,
"name": "好久好久",
"adr": {
"class": "com.xxx.ai.commonapi.entity.Adress",
"homeadress": "123",
"companyadress": "456"
},
"child": {
"son": "haha",
"daughter": "xixi"
},
"ele":
["1", "2"]
}
data4 = {
"class": "com.xxx.ai.commonapi.entity.User",
"userid": 2016020201809846,
"name": "好久好久",
"adr": {
"class": "com.xxx.ai.commonapi.entity.Adress",
"homeadress": "123",
"companyadress": "456"
},
"child": {
"son": "haha",
"daughter": "xixi",
"adr": {
"class": "com.xxx.ai.commonapi.entity.Adress",
"homeadress": "123",
"companyadress": "456"
}
},
"ele":
["1", "2"]
}
print(conn.do(command_str1))
print( conn.invoke("com.xxx.ai.commonapi.MatchFlightPartternService","matchuser",data1))
print( conn.invoke("com.xxx.ai.commonapi.MatchFlightPartternService","matchuser",data2))
print( conn.invoke("com.xxx.ai.commonapi.MatchFlightPartternService","matchuser",data3))
print( conn.invoke("com.xxx.ai.commonapi.MatchFlightPartternService","matchuser",data4))