Python Learning notes----URLLIB2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.