好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python接口自动化

requests库

requests库介绍:

Requests是一个优雅而简单的Python HTTP库,专为人类而构建。(来自官方的介绍)

requests 是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,很适合做接口测试。

request库使用

requests库安装:

requests是第三方的库,需要先安装 :pip3 install requests(python3版本)

requests源码

 1 def request(method, url, **kwargs):
 2     """Constructs and sends a :class:`Request <Request>`.
 3 
 4     :param method: method for the new :class:`Request` object.
 5     :param url: URL for the new :class:`Request` object.
 6     :param params: (optional) Dictionary, list of tuples or bytes to send
 7         in the query string for the :class:`Request`.
 8     :param data: (optional) Dictionary, list of tuples, bytes, or file-like
 9         object to send in the body of the :class:`Request`.
10     :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
11     :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
12     :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
13     :param files: (optional) Dictionary of ``‘name‘: file-like-objects`` (or ``{‘name‘: file-tuple}``) for multipart encoding upload.
14         ``file-tuple`` can be a 2-tuple ``(‘filename‘, fileobj)``, 3-tuple ``(‘filename‘, fileobj, ‘content_type‘)``
15         or a 4-tuple ``(‘filename‘, fileobj, ‘content_type‘, custom_headers)``, where ``‘content-type‘`` is a string
16         defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
17         to add for the file.
18     :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
19     :param timeout: (optional) How many seconds to wait for the server to send data
20         before giving up, as a float, or a :ref:`(connect timeout, read
21         timeout) <timeouts>` tuple.
22     :type timeout: float or tuple
23     :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
24     :type allow_redirects: bool
25     :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
26     :param verify: (optional) Either a boolean, in which case it controls whether we verify
27             the server‘s TLS certificate, or a string, in which case it must be a path
28             to a CA bundle to use. Defaults to ``True``.
29     :param stream: (optional) if ``False``, the response content will be immediately downloaded.
30     :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert‘, ‘key‘) pair.
31     :return: :class:`Response <Response>` object
32     :rtype: requests.Response
33 
34     Usage::
35 
36       >>> import requests
37       >>> req = requests.request(‘GET‘, ‘https://httpbin.org/get‘)
38       <Response [200]>
39     """
40 
41     # By using the ‘with‘ statement we are sure the session is closed, thus we
42     # avoid leaving sockets open which can trigger a ResourceWarning in some
43     # cases, and look like a memory leak in others.
44     with sessions.Session() as session:
45         return session.request(method=method, url=url, **kwargs)
View Code

 其中,month主要是用get和post

requests模块发送请求有data、json、params三种携带参数的方法。

params在get请求中使用,data、json在post请求中使用。

‘‘‘requests--get方法r=requests.get(‘https://movie.douban.com/‘)print(r.status_code)    #查看状态码print(r.text)   #查看响应内容print(r.content.decode(‘utf-8‘))    #查看字节流(二进制)print(r.url)    #查看请求地址print(r.encoding)   #查看字符编码print(r.cookies)    #查看cookieprint(r.headers)    #查看请求头信息# 当返回的响应格式是json时,可以用r.json())# 这里有两种方式,一种是直接用请求地址,另一种是用参数的方式传递paramesurl=‘https://movie.douban.com/subject_search?search_text=%E5%93%AA%E5%90%92&cat=1002‘url1=‘https://movie.douban.com/subject_search?search_text=哪吒&cat=1002‘payload={‘search_text‘:‘哪吒‘,‘cat‘:1002}r=requests.get(url1,params=payload)‘‘‘

查看更多关于Python接口自动化的详细内容...

  阅读:17次