本文整理汇总了Python中jsonrpclib.ProtocolError方法的典型用法代码示例。如果您正苦于以下问题:Python jsonrpclib.ProtocolError方法的具体用法?Python jsonrpclib.ProtocolError怎么用?Python jsonrpclib.ProtocolError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonrpclib
的用法示例。
在下文中一共展示了jsonrpclib.ProtocolError方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: applyChanges
# 需要导入模块: import jsonrpclib [as 别名]
# 或者: from jsonrpclib import ProtocolError [as 别名]
def applyChanges(aclName, apiEndpoints, aclRules):
""" Given the switch mapping and a list of the new ACL rules, apply
the ACL to each switch """
cmdList = ["enable",
"configure",
# Not the most efficient way to clear an ACL:
"no ip access-list %s" % aclName,
# Now enter configuration mode for the ACL:
"ip access-list %s" % aclName]
cmdList = cmdList + aclRules + ["exit"]
for hostname, apiEndpoint in apiEndpoints.iteritems():
print "Updating access list on switch", hostname, "....",
try:
apiEndpoint.runCmds(1, cmdList)
except jsonrpclib.ProtocolError as e:
print "[ERROR]"
print " ", e
# jsonrpclib isn't very friendly at getting the error data as
# specified by the spec. This is a shortcut for getting the
# last error:
errorResponse = jsonrpclib.loads(jsonrpclib.history.response)
print " Details:", errorResponse["error"]["data"][-1]["errors"]
else:
print "[SUCCESS]"
示例2: run_commands
# 需要导入模块: import jsonrpclib [as 别名]
# 或者: from jsonrpclib import ProtocolError [as 别名]
def run_commands(self, commands, version=1, auto_format=False, format='json', timestamps=True):
"""
This method will run as many commands as you want. The 'enable' command will be prepended automatically so you
don't have to worry about that.
:param commands: List of commands you want to run
:param version: Version of the eAPI you want to connect to. By default is 1.
:param auto_format: If set to True API calls not supporting returning JSON messages will be converted automatically to text. By default is False.
:param format: Format you want to get; 'json' or 'text'. By default is json. This will trigger a CommandUnconverted exception if set to 'json' and auto_format is set to False. It will return text if set to 'json' but auto_format is set to True.
:param timestamps: This will return some useful information like when was the command executed and how long it took.
"""
if 'enable' is not commands[0]:
commands.insert(0, 'enable')
if auto_format:
format = 'json'
try:
result = self.device.runCmds(
version=version,
cmds=commands,
format=format,
timestamps=timestamps,
)
except ProtocolError as e:
code = e[0][0]
error = e[0][1]
if code == 1003:
# code 1003 means the command is not yet converted to json
if auto_format:
result = self.device.runCmds(
version=version,
cmds=commands,
format='text',
timestamps=timestamps
)
else:
raise exceptions.CommandUnconverted(error)
# code -32602 means "Unexpected parameter 'timestamps' for method 'runCmds' provided"
elif code == -32602:
result = self.device.runCmds(
version=version,
cmds=commands,
format=format
)
elif code == 1002:
# code 1002 means the command was wrong
raise exceptions.CommandError(error)
elif code == 1000:
# code 1000 means a command is wrong when doing a "config replace"
raise exceptions.ConfigReplaceError(e)
else:
raise exceptions.UnknownError((code, error))
return result