程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python request Library

編輯:Python

In the face of http Of api When testing , Or make use of api Realize batch adding 、 Delete and so on , It is necessary to make use of a transmission http Requested Library , Today, I will introduce a library that I feel is quite simple in the process of using -- requests

install

python -m pip install requests

requests Support python The version is 2.7 as well as 3.5+

The module specification

For sending a http(s) request , does

  • Send a request
  • Processing of sending results , Simple can be divided into the following two parts
    • After sending successfully , Process the received data
    • fail in send , Handling of abnormal conditions

First of all, it needs to be clear , requests Is in urllib3 This is very powerful python It has been repackaged on the basis of its own library , And provide users with a more concise , Use a more convenient interface

and request What interfaces does it provide ?

from .models import Request, Response, PreparedRequest
from .api import request, get, head, post, patch, put, delete, options
from .sessions import session, Session
from .status_codes import codes
from .exceptions import (
RequestException, Timeout, URLRequired,
TooManyRedirects, HTTPError, ConnectionError,
FileModeWarning, ConnectTimeout, ReadTimeout
)

The above part mainly involves several classes that may be used Request, Response, PreparedRequest, Session

Others are basically descriptions of functions and exceptions

So the point goes back to the above mentioned 4 Classes

Session Be responsible for creating a connection with the other party ,Request Be responsible for encapsulating the information to be sent , PreparedRequest Responsible for Request Encapsulated information preprocessing ,Response Mainly the information returned by the request

That is to say , Session Create a connection in , send out ( send ) Request, obtain Response

This article is mainly a simple introduction , Don't give a detailed introduction , After that, I will analyze the source code completely .

Don't be on it 4 A class is scared , In real use , In fact, it is very simple

send out

If you look at the upper part , Will notice

from .api import request, get, head, post, patch, put, delete, options

In fact, these functions can realize our sending function , First, explain a few interfaces

get, head, post, patch, put, delete, options These types of sending actually use request Interface , It's nothing more than calling request Function , Specify your own method

def get(url, params=None, **kwargs):
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)

So the focus of the interface is request

"""Constructs and sends a :class:`Request <Request>`.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{
'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response

There are a lot of official documents , I'll pick a few that may be used more frequently in daily life

  • methon Fill in the type of request in , for example GET, POST, DELETE wait , see get() And so on , Lowercase is fine
  • url Fill in the fields http Normal address of
  • Then there are the extensible extra fields
    • header Put some request header information in the , For example, specify the parsing method Content-Type:application/json, How to connect Connection:keep-alive And so on are usually placed in header in
    • Post In request Body The content of the body is data perhaps json
    • timeout Custom request timeout

Example :

import requests
req = requests.request('GET', 'https://www.baidu.com')
print(req.status_code)

Because the return is a Response object , What specific information can be obtained from it , You can jump directly to Response View in class of

Handling of replies

It is used for general communication json Format , To transmit data , Suppose we here url You can get one json For example :

req = requests.request('GET', 'https://www.baidu.com')
if req.status_code == 200 :
try:
res_json = req.json()
except ValueError:
print(" Parse failure ")

there req.json(), It is actually the result of the return req.text Did it once json.loads To deal with , So the most basic thing to do is

req = requests.request('GET', 'https://www.baidu.com')
if req.status_code == 200 :
print(req.text)

abnormal

The exception was mentioned in the previous introduction

from .exceptions import (
RequestException, Timeout, URLRequired,
TooManyRedirects, HTTPError, ConnectionError,
FileModeWarning, ConnectTimeout, ReadTimeout
)

Be careful. , The timeout here is also an exception , So when we send a request, if we set Timeout time It needs to pass try ... except To capture

import requests
try:
req = requests.request('GET', 'https://github.com/',timeout=1)
except requests.exceptions.Timeout:
print(" request timeout ")

summary

Although what I write here is very simple , But the parameters that send the request can actually do a lot of things , File upload and download , Setting agent ,Cookies wait , Interested parties can refer to the official documents

https://requests.readthedocs.io/

requests It's easy to use , It's also powerful , It is a library worth learning ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved