HTTP library requests developed by Python, pythonrequests

Source: Internet
Author: User

HTTP library requests developed by Python, pythonrequests

Requests is an HTTP library developed based on Python using The Apache2 Licensed license. It is highly encapsulated based on the Python built-in module, which makes network Requests by Pythoner much better, using Requests can easily complete any operations available to the browser.

1. GET request

#1. No parameter instance import requests ret = requests. get ('https: // github.com/timeline.json') print (ret. url) print (ret. text) #2. There is a parameter instance import requests payload = {'key1': 'value1 ', 'key2': 'value2'} ret = requests. get ("http://httpbin.org/get", params = payload) print (ret. url) print (ret. text)

2. POST request

#1. Basic POST instance import requests payload = {'key1': 'value1', 'key2': 'value2'} ret = requests. post ("http://httpbin.org/post", data = payload) print (ret. text) #2. Send the request header and data instance import requests import json url = 'https: // api.github.com/some/endpoint' payload = {'some ': 'data'} headers = {'content-type': 'application/json'} ret = requests. post (url, data = json. dumps (payload), headers = headers) print (ret. text) print (ret. cookies)

3. Other requests

Requests. get (url, params = None, ** kwargs) requests. post (url, data = None, json = None, ** kwargs) requests. put (url, data = None, ** kwargs) requests. head (url, ** kwargs) requests. delete (url, ** kwargs) requests. patch (url, data = None, ** kwargs) requests. options (url, ** kwargs) # The preceding methods are used to construct requests based on this method. request (method, url, ** kwargs)

4. Request Parameters

def request(method, url, **kwargs):  """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 or bytes to be sent in the query string for the :class:`Request`.  :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.  :param json: (optional) json data 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 long 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. Set to True if POST/PUT/DELETE redirect following is allowed.  :type allow_redirects: bool  :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.  :param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. 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  Usage::   >>> import requests   >>> req = requests.request('GET', 'http://httpbin.org/get')   <Response [200]>  """

5. Parameter example

Def param_method_url (): # requests. request (method = 'get', url = 'HTTP: // 127.0.0.1: 8000/test/') # requests. request (method = 'post', url = 'HTTP: // 127.0.0.1: 8000/test/') passdef param_param (): #-it can be a dictionary #-it can be a string #-it can be a byte (within ascii encoding) # requests. request (method = 'get', # url = 'HTTP: // 127.0.0.1: 8000/test/', # params = {'k1': 'v1 ', 'k2 ': 'utilities '}) # requests. request (method = 'get', # url = 'HTTP: // 127.0.0.1: 8000/test /', # Params = "k1 = v1 & k2 = Utilities & k3 = v3 & k3 = vv3") # requests. request (method = 'get', # url = 'HTTP: // 127.0.0.1: 8000/test /', # params = bytes ("k1 = v1 & k2 = k2 & k3 = v3 & k3 = vv3", encoding = 'utf8') # Error # requests. request (method = 'get', # url = 'HTTP: // 127.0.0.1: 8000/test /', # params = bytes ("k1 = v1 & k2 = Utilities & k3 = v3 & k3 = vv3", encoding = 'utf8') passdef param_data (): # It Can Be A dictionary # It Can Be A string # It Can Be A byte # It Can Be A file object # requests. request (method = 'post ',# Url = 'HTTP: // 127.0.0.1: 8000/test/', # data = {'k1': 'v1 ', 'k2': 'utilities'}) # requests. request (method = 'post', # url = 'HTTP: // 127.0.0.1: 8000/test/', # data = "k1 = v1; k2 = v2; k3 = v3; k3 = v4 "#) # requests. request (method = 'post', # url = 'HTTP: // 127.0.0.1: 8000/test/', # data = "k1 = v1; k2 = v2; k3 = v3; k3 = v4 ", # headers = {'content-type': 'application/x-www-form-urlencoded'} #) # requests. request (method = 'post', # url = 'HTTP :/ /127.0.0.1: 8000/test/', # data = open ('data _ file. py', mode = 'R', encoding = 'utf-8'), # The file content is: k1 = v1; k2 = v2; k3 = v3; k3 = v4 # headers = {'content-type': 'application/x-www-form-urlencoded'} #) passdef param_json (): # serialize the corresponding data in json into a string, json. dumps (...) # It is then sent to the server body, and the Content-Type is {'content-type': 'application/json'} requests. request (method = 'post', url = 'HTTP: // 127.0.0.1: 8000/test/', json = {'k1': 'v1 ', 'K2': 'utilities '}) def param_headers (): # send the request header to the server requests. request (method = 'post', url = 'HTTP: // 127.0.0.1: 8000/test/', json = {'k1': 'v1 ', 'k2 ': 'utilities '}, headers = {'content-type': 'application/x-www-form-urlencoded'}) def param_cookies (): # send cookies to the server requests. request (method = 'post', url = 'HTTP: // 127.0.0.1: 8000/test/', data = {'k1': 'v1 ', 'k2 ': 'v2'}, cookies = {'cook1': 'value1'},) # You can also use CookieJar (in the dictionary format: From http. cookiejar import CookieJar from http. cookiejar import Cookie obj = CookieJar () obj. set_cookie (Cookie (version = 0, name = 'c1', value = 'v1 ', port = None, domain = '', path ='/', secure = False, expires = None, discard = True, comment = None, comment_url = None, rest = {'httponly ': None}, rfc2109 = False, port_specified = False, domain_specified = False, domain_initial_dot = False, path_specified = False) reques Ts. request (method = 'post', url = 'HTTP: // 127.0.0.1: 8000/test/', data = {'k1': 'v1 ', 'k2 ': 'v2'}, cookies = obj) def param_files (): # send files # file_dict = {# 'f1': open ('readme ', 'rb ') #}# requests. request (method = 'post', # url = 'HTTP: // 127.0.0.1: 8000/test/', # files = file_dict) # send a file, custom file name # file_dict = {# 'f1': ('test.txt ', open ('readme', 'rb') #}# requests. request (method = 'post', # url = 'HTTP: // 127.0.0. 1: 8000/test/', # files = file_dict) # Send File, custom file name # file_dict = {# 'f1': ('test.txt', "hahsfaksfa9kasdjflaksdjf") #}# requests. request (method = 'post', # url = 'HTTP: // 127.0.0.1: 8000/test/', # files = file_dict) # send a file, custom file name # file_dict ={# 'f1': ('test.txt ', "hahsfaksfa9kasdjflaksdjf", 'application/text', {'k1': '0'}) #}# requests. request (method = 'post', # url = 'HTTP: // 127.0.0.1: 8000/test/', # files = File_dict) passdef param_auth (): from requests. auth import HTTPBasicAuth, HTTPDigestAuth ret = requests. get ('https: // api.github.com/user', auth = HTTPBasicAuth ('wupeiqi ', 'sdfasdfasdf') print (ret. text) # ret = requests. get ('HTTP: // 192.168.1.1 ', # auth = HTTPBasicAuth ('admin', 'admin') # ret. encoding = 'gbk' # print (ret. text) # ret = requests. get ('HTTP: // httpbin.org/digest-auth/auth/user/pass', auth = HTTPDigestAuth ('user', 'pass') # print (ret) # def param_timeout (): # ret = requests. get ('HTTP: // google.com/', timeout = 1) # print (ret) # ret = requests. get ('HTTP: // google.com/', timeout = (5, 1) # print (ret) passdef param_allow_redirects (): ret = requests. get ('HTTP: // 127.0.0.1: 8000/test/', allow_redirects = False) print (ret. text) def param_proxies (): # proxies = {# "http": "61.172.249.96: 80", # "http S ":" http: // 61.185.219.126: 3128 ", #}# proxies = {'HTTP: // 10.20.1.128 ': 'http: // 10.10.1.10: 5323 '} # ret = requests. get ("http://www.proxy360.cn/Proxy", proxies = proxies) # print (ret. headers) # from requests. auth import HTTPProxyAuth # proxyDict = {# 'http': '77. 75.105.165 ', # 'https': '77. 75.105.165 '#}# auth = HTTPProxyAuth ('username', 'mypassword') # r = requests. get ("http://www.google.com ", Proxies = proxyDict, auth = auth) # print (r. text) passdef param_stream (): ret = requests. get ('HTTP: // 127.0.0.1: 8000/test/', stream = True) print (ret. content) ret. close () # from contextlib import closing # with closing (requests. get ('HTTP: // httpbin.org/get', stream = True) as r: # process the response here. # For I in r. iter_content (): # print (I) def requests_session (): import requests session = requests. session () ### 1. First, log on to any page and obtain cookie i1 = session. get (url = "http://dig.chouti.com/help/service") ### 2, the user login, carrying the last cookie, the background in the cookie gpsd authorization i2 = session. post (url = "http://dig.chouti.com/login", data = {'phone': "8615131255089", 'Password': "xxxxxx", 'ononth': ""}) i3 = session. post (url = "http: // dig. Chouti.com/link/vote? LinksId = 8589623 ",) print (i3.text)

6. Simulate login of requests to GitHub

Import requests from bs4 import BeautifulSoup def login_github (): "log on to GitHub: return:" "# obtain csrf_token r1 = requests through a simulated browser using the requests module. get ('https: // github.com/login') # get object s1 = BeautifulSoup (r1.text, 'html. parser ') # Use bs4 to parse the HTML object token = s1.find ('input', attrs = {'name': 'authenticity _ token '}). get ('value') # obtain the logon authorization code, that is, csrf_token get_cookies = r1.cookies. get_dict () # obtain the cookies of the get request. The # Send post login request ''' post login parameter commit Sign + in utf8 must authenticity_token E961jQMIyC9NPwL54YPj70gv2hbXWJ... FTUd + e4lT5RAizKbfzQo4eRHsfg = login JackUpDown (User Name) password ********** (password) ''' r2 = requests. post ('https: // github.com/session', data = {'commit ': 'sign + in', 'utf8': 'authorization', 'authenticity _ token': token, 'login': 'jackupdown ', 'Password':' ************ '}, cookies = get_cookies # cookies carrying get requests) login_cookies = r2.cookies. get_dict () # obtain the successfully logged-on cookie. With this cookie, you can access any GitHub page # Bring post cookies to any page r3 = requests. get ('https: // github.com/settings/emails', cookies = login_cookies) print (r3.text)

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.