Introduction to the interface of Python requests

Source: Internet
Author: User
Tags connection pooling

Python Requests Introduction

Introduction to the official website

Requests is the only non-GMO Python HTTP Library that humans can enjoy safely.

Requests allows you to send pure natural, plant-bred http/1.1 requests without manual labor. You do not need to manually add query strings to URLs, nor do you need to encode your POST data. The keep-alive and HTTP connection pooling functions are 100% automated, all powered by URLLIB3 rooted in the requests interior.

Functional characteristics

Requests fully meets the needs of today's web.

    • Keep-alive & Connection Pool
    • Internationalized Domain names and URLs
    • Session with persistent Cookie
    • Browser-type SSL authentication
    • Automatic content decoding
    • Basic/digest type of identity authentication
    • Elegant Key/value Cookies
    • Automatic decompression
    • Unicode Response Body
    • HTTP (S) proxy support
    • File chunked upload
    • Shed
    • Connection timed out
    • chunked Request
    • Support.netrc

The requests supports Python 2.6-2.7 and 3.3-3.7, and works perfectly under PyPy.

"' Requests interface introduction  '

#-*-coding:utf-8-*-#TIME:2017/10/12 22:54#Author:yangyangjun#start the Requests learning journey, where you write the text directly in the code. #Requests is a third-party library of Python, so it needs to be installed#execute PIP install requests directly in the Command line window#If you are prompted to fail the installation, you can check the prompts, install the prerequisite software and reinstall#can be imported directly after successful installationImportRequests#If you do not error, you can#References#visit the website http://cn.python-requests.org/zh_CN/latest/to view the official Chinese documents#or view source code https://github.com/requests/requests#can also go to their own installation directory to find d:/sprogram/python/lib/site-packages/requests/api.py and other files to view the introduction" "Requests Interface Introduction" "#This part of the document contains all the interfaces of the requests. For the External Libraries section of requests dependencies, we present the most important sections here and provide links to the canonical documentation. # #Main interface#requests all functions can be accessed through the following 7 methods. All of them will return an instance of the Response object. #1. Request () method#requests.request (method, URL, **kwargs) constructs and sends a request. Constructs and sends requests. #here the parameter method, URL is a required parameter, **kwargs is an optional parameter, is a variable keyword parameter, Dictionary type" "Parameter: Method--method for the new Request object.  The method URL of the new Request object-the URL for the.  The urlparams of the new Request object--(optional) Dictionary or bytes to is sent in the query string for the request. (optional) The dictionary or byte that is sent for the request in the query string. Data--(optional) Dictionary or list of tuples [(key, value)] (would be form-encoded), bytes, or File-like object to send In the body of the Request. (optional) A dictionary or tuple list [(key, value)] (which encodes the form), bytes, or similar files to the request body. JSON--(optional) JSON data to send in the body of the Request. Optionally, the JSON data is sent to the request body. Headers--(optional) Dictionary of HTTP headers to send with the Request. (optional) The HTTP header dictionary to send the request. Cookies--(optional) Dict or Cookiejar object to send with the Request. (optional) command or Cookiejar object to send the request. 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.438/5000 (optional) Dictionary of "name": File-Like-object (or {' Name ': File-tuple}) for multipart encoding uploads. 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 that defines the content type and custom_headers of the given file, which contains a dict-like object that contains an additional header file for adding files. Auth--(optional) Auth tuple to enable Basic/digest/custom HTTP auth. (optional) Auth tuple to enable basic/digest/custom HTTP auth. Timeout (float or tuple)--(optional) How many seconds to wait for the server to send data before giving up, as a float, or a (Connect timeout, read timeout) tuple. (optional) The number of seconds to wait for the server to send data before discarding, either as a float or as a tuple (connection timeout, read timeout). Allow_redirects (BOOL)--(optional) Boolean. Enable/disable Get/options/post/put/patch/delete/head redirection. Defaults to True. (optional) Boolean. Enable/Disable Get/Options/post/put/patch/delete/head redirection. The default value is true. Proxies--(optional) Dictionary mapping protocol to the URLof the proxy. (optional) Dictionary mapping Protocol to the URL of the proxy. Verify--(optional) either a Boolean, in which case it controls whether we verify the server ' s TLS certificate, or a stri Ng, in which case it must is a path to a CA bundle to use. Defaults to True. (optional) A Boolean value, in which case it controls whether we validate the server's TLS certificate, or a string, in which case it must be a path to the CA package to use. The default value is true. Stream--(optional) If False, the response content would be immediately downloaded. (optional) If an error occurs, the response content will be downloaded immediately. Cert--(optional) if String, path to SSL client cert file (. pem). If Tuple, (' cert ', ' key ') pair. (optional) If the string, path to the SSL client cert file (. pem). If it is a tuple (' cert ', ' key ') pair." "#request () method instancereq = Requests.request ('GET','Http://httpbin.org/get')Printreq#Get <response [200]>#2. Head () method#requests.head (URL, **kwargs)#sends a HEAD request. Send a head request. #Optional arguments that ' request ' takes. The optional parameters that are requested for the fetch. R"""sends a HEAD request.   :p Aram Url:url for the New:class: ' Request ' object.     :p Aram \*\*kwargs:optional arguments that ' request ' takes. : Return:: Class: ' Response <Response> ' object:rtype:requests. Response"""#Head () method instancereq = Requests.head ('Http://httpbin.org/get')Printreq#Get <response [200]>#3. Get () method#requests.get (URL, Params=none, **kwargs) [Source code]#sends a GET request.##Parameters:#URL--URL for the new Request object.#params--(optional) Dictionary or bytes to being sent in the query string for the Request.#**kwargs--Optional arguments that request takes.#return:#Response Object##return type:#requests. Response#get () method instancereq = Requests.get ('Http://httpbin.org/get')Printreq#Get <response [200]>#4. Post () method#requests.post (URL, Data=none, Json=none, **kwargs) [Source code]#sends a POST request.##Parameters:#URL--URL for the new Request object.#Data--(optional) Dictionary (would be form-encoded), bytes, or File-like object to send in the body of the request.< /c4>#JSON--(optional) JSON data to send in the body of the Request.#**kwargs--Optional arguments that request takes.#return:#Response Object##return type:#requests. Response#post () method instancereq = Requests.post ('Http://httpbin.org/get')Printreq#Get <response [405]>#5. Put () method#requests.put (URL, Data=none, **kwargs) [Source code]#sends a PUT request.##Parameters:#URL--URL for the new Request object.#Data--(optional) Dictionary (would be form-encoded), bytes, or File-like object to send in the body of the request.< /c8>#JSON--(optional) JSON data to send in the body of the Request.#**kwargs--Optional arguments that request takes.#return:#Response Object##return type:#requests. Response#6. Patch () method#requests.patch (URL, Data=none, **kwargs) [Source code]#sends a PATCH request.##Parameters:#URL--URL for the new Request object.#Data--(optional) Dictionary (would be form-encoded), bytes, or File-like object to send in the body of the request.< /c2>#JSON--(optional) JSON data to send in the body of the Request.#**kwargs--Optional arguments that request takes.#return:#Response Object##return type:#requests. Response#7. Delete () method#requests.delete (URL, **kwargs) [Source code]#sends a DELETE request.##Parameters:#URL--URL for the new Request object.#**kwargs--Optional arguments that request takes.#return:#Response Object##return type:#requests. Response#

Introduction to the interface of Python requests

Related Article

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.