It is relatively simple to query some parameters on the network device through the interface method, which is implemented directly on the Linux server with Curl before operation. But for the Python Automation script implementation, the Curl command is less useful.
Like what:
curl-k-u user:password h ttps://192.168. 254.4:8100/api/tmcm/2.0/ Bandwidth_pack_license_key | Json_reformat
This will return the value you need to get.
If you use Python, you'll call the URLLIB2 library. Try to encapsulate a URL query function to achieve the same effect as the above Curl command.
1, the first to import the corresponding library.
Import Json,urllib2
2, define the function, enter the URL to return the corresponding value, and do the exception processing.
def conntmcm (URI):
Try
url = "Https://192.168.254.4:8100"
Username = "User"
Password = "Password"
Passwordmgr = Urllib2. Httppasswordmgrwithdefaultrealm ()
Passwordmgr.add_password (None, URL, username, password)
Handler = Urllib2. Httpbasicauthhandler (Passwordmgr)
Opener = Urllib2.build_opener (handler)
Urllib2.install_opener (opener)
if bool (URI)! = False:
URL + = URI
Response = Urllib2.urlopen (URL)
Lblist = Response.read ()
JList = Json.loads (lblist)
Return JList
Except
info = Sys.exc_info ()
Lblist = Getallattrs (info[0]) + getallattrs (info[1])
Return lblist
3. Calling functions
Lblicensedict = CONNTMCM ('/api/tmcm/2.0/bandwidth_pack_license_key ')
This article is from the "12028998" blog, please be sure to keep this source http://12038998.blog.51cto.com/12028998/1854503
Python Learning notes----URLLIB2