公司后端服務使用 java 重構后,很多接口采用了阿里的 dubbo 協議。而 python 是無法直接調用 dubbo 接口的,但可以通過 telnet 調用,具體可以通過 telnetlib 模塊的 Telnet類 來調用,只需要四行代碼即可實現:
1
2
3
4
5
6
7
8
9
10
|
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( '\r\n' )[ 0 ] |
分裝成類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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( '\r\n' )[ 0 ] return data |
以上就是python 如何調用 dubbo 接口的詳細內容,更多關于python 調用 dubbo 接口的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/shenh/p/12796020.html