本文整理匯總了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