我有以下代码:
import xmlrpc.client as xc
class AristaSwitch():
def __init__(self,devicename,user='admin',password='xxxxxx')
self.url="https://"+user+":"+password+"@"+devicename+"/command-api"
self.Server = xc.Server(self.url) **<----i know this is not correct**
more code below here
我希望能够编写如下代码:
as = AristaSwitch("192.168.1.1")
as.runCmds(1, [ "show hostname" ] )
他们的做法是:
import xmlrpc.client as xc
switch = xc.Server( "https://admin:[email protected]/command-api" )
response = switch.runCmds( 1, [ "show hostname" ] )
更新我认为将它添加到 init 函数应该这样做
self.InitializeRPCServer()
def InitializeRPCServer():
switch=xc.Server(self.url)
return switch
看来你只是想环绕xc.Server
. 只需使用函数而不是类。
import xmlrpc.client as xc
def AristaSwitch(devicename, user='admin', password='xxxxxx'):
url="https://"+user+":"+password+"@"+devicename+"/command-api"
Server = xc.Server(url)
return Server
然后做你的事情:
as = AristaSwitch("192.168.1.1")
as.runCmds(1, [ "show hostname" ] )
如果您正在谈论自定义xc.Server
对象,您可以继承它:
class AristaSwitch(xc.Server):
def __init__(self, devicename, user='admin', password='xxxxxx'):
self.url="https://"+user+":"+password+"@"+devicename+"/command-api"
super().__init__(self.url)
您需要将 更新def __init__
为您的自定义url
输入,但您应该对原始实现相当了解,因为您可能会无意中覆盖超类中的某些属性或实现细节xc.Server
。
在这个用例中,AristaSwitch
基本上xc.Server
是一个自定义的实例化方法,如果你愿意,你可以稍后用你自己的方法来补充它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句