current position:Home>Analysis of Python requests module

Analysis of Python requests module

2022-01-30 21:07:47 Little cute in the circle of friends

This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge

Review

Python Object oriented high level language . We have learned before Python It also supports the network request module .

  • urllib modular On the basic url Request to provide request、error、parse、robot.txt Module method
  • urllib3 modular Provide thread safe pool management and proxy service related methods

In this issue , We'll learn about the... Used at the bottom urllib3 Module's third-party network request Python modular -requests,Let's go~

1. requests The module overview

requests Modules in On its official website It says “ Give Way HTTP Serving humanity ”. therefore ,requests The module also adheres to this principle , Provide more convenient HTTP Request method .

image.png

In our code HTTP When you ask , If not handled properly, there will be security vulnerabilities 、 Code redundancy and other problems

  • requests Module features

    • The bottom floor is adopted, and the interior is adopted urllib3 Modules are packaged , More user-friendly and convenient

      • We can see by looking at the source code requests Modules are indeed imported urllib3 Modular

      image.png

    • Allow users to communicate in different ways HTTP Link request , Than urllib2 Module operation is more concise

    • Provide keep-alive And connection pool , Automation remains connected

    • Support cookie Keep talking

    • Support file upload 、 Automatic response content encoding

  • requests Module acquisition

    • Mode one : Use pip Direct download requests modular

      pip install requests
       Copy code 
    • Mode two : Use git Tool execution clone download

      git clone git://github.com/kennethreitz/requests.git
       Copy code 
  • requests Module USES

    We need to use... In our code import Import requests modular

    import requests
     Copy code 

2. requests Related methods

requests Module is used Apache2 licensed Permit HTTP library , In the user manual, we can see that the main methods are as follows

Method effect
requests.get(url) send out get request
requests.post(url) send out post request
requests.put(url) send out put request
requests.delete(url) send out delete request
requests.head(url) send out head request
requests.options(url) send out options request

requests The module provides a description of the request method field

Field explain
url Network request link
data Requested data , Data can be dictionaries 、 Tuples
param Pass on... In the form of a dictionary URL Parameters
header HTTP Request header
files Multiple encoded files
allow_redirects Redirect
timeout Overtime

requests Module object attribute description

Field explain
text What the server returned
encoding Encrypted format
content The content of the request response
json Json Decode the content of the response
raw The content of the server's original response ( Binary system )
status_code Response status code
raise_for_status Abnormal status code
headers Response header information
cookie Responsive cookie Information

3. requests Starting steps

requests Used at the bottom of the module urllib3 modular , Highly encapsulate the requested method , Let's summarize the operation steps :

  • Import requests modular

    import requests
     Copy code 
  • Define a requests object , Such as calling get Request Nuggets website

    r = requests.get("https://juejin.cn/user/211521683863847/posts")
     Copy code 
  • Query the relevant information in the request response

    print("status_code",r.status_code)
    print("content",r.content.decode("utf-8"))
    print("headers['Content-type']:",re.headers['Content-type'])
     Copy code 

4. A profound

according to requests The request method provided by the module , Let's implement a working scenario :

Gave a si no interface , Let you test :

Interface :/action/openapi/post_list Interface parameters :

Parameters Mandatory Type and scope explain The default value is
access_token true string oauth2_token Acquired access_token
catalog false int Category ID 1- Question and answer 2- Share 3-IT Chowder ( comprehensive ) 4- depot 100- Career 0- all 0
tag false string Post related tags
pageSize false int Number of entries per page 20
page/pageIndex false int the number of pages 1
dataType true string Return data type ['json' 'jsonp'

Interface request method :POST

First , We follow the interface request mode , Create a do_post Method

def do_post(ip,api,data):

    headers = {"Content-Type": "application/json"}

    url = "http://"+ip+api

    print (url)

    try:
        r = requests.post(url,headers = headers,data = json.dumps(data),timeout= 10)
        print(r.status_code)
        print(r.content.decode("utf-8"))

    except requests.exceptions.ConnectionError:
        requests.status_code = "Connection refused"
    except requests.exceptions.ReadTimeout:
        requests.status_codes = "Read timed out"
 Copy code 

testing procedure :

data = {}
api_pose = ""
do_post(ip,api_pose,data)
 Copy code 

summary

In this issue , We are right. requests Module requests methods and related attributes for learning ,requests Module is based on urllib3 encapsulate , Give Way HTTP It's more convenient to ask .

The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~

copyright notice
author[Little cute in the circle of friends],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302107441221.html

Random recommended