Several ways that Python calls API interfaces

Source: Internet
Author: User
Tags urlencode

I believe that the students who have done the automation operation have used API interface to accomplish certain actions. API is a set of mature system necessary interface, can be called by other systems or scripts, which is also a compulsory operation of automation.


This article introduces several ways to invoke the API in Python, and here are the libraries that will be used in Python.

-Urllib2

-Httplib2

-Pycurl

-Requests


    1. Urllib2

Import urllib2, Urllibgithub_url = ' https://api.github.com/user/repos ' Password_manager = urllib2. Httppasswordmgrwithdefaultrealm () Password_manager.add_password (None, Github_url, ' user ', ' * * * ') auth = urllib2. Httpbasicauthhandler (Password_manager) # Create an authentication Handleropener = Urllib2.build_opener (auth) # Create an Opener with the authentication Handlerurllib2.install_opener (opener) # Install the opener ... request = Urllib2. Request (Github_url, Urllib.urlencode ({' name ': ' Test repo ', ' description ': ' Some Test Repository '}) # Manual encoding Requiredhandler = Urllib2.urlopen (Request) print handler.read ()

2. Httplib2

Import urllib, Httplib2github_url = ' H = httplib2. Http (". Cache") h.add_credentials ("User", "******", "data = Urllib.urlencode ({" Name ":" Test "}) resp, content = H.request ( Github_url, "POST", data) print content

3. Pycurl

Import Pycurl, Jsongithub_url = "User_pwd =" user:***** "data = Json.dumps ({" Name ":" Test_repo "," description ":" Some test Repo "}) c = Pycurl. Curl () c.setopt (Pycurl. URL, Github_url) c.setopt (Pycurl. Userpwd, User_pwd) c.setopt (Pycurl. POST, 1) c.setopt (Pycurl. Postfields, data) C.perform ()

4. Requests

Import requests, Jsongithub_url = "data = Json.dumps ({' name ': ' Test ', ' description ': ' Some Test Repo '}) R = Requests.post (gi Thub_url, data, auth= (' user ', ' * * * * ')) print R.json

There are several ways to invoke the API to perform the action, but requests is the simplest, clearest, and recommended way to use this code.

This article from the "Linux and Network" blog, reproduced please contact the author!

Several ways that Python calls API interfaces

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.