首页 > 编程语言 > 详细

Python接口自动化

时间:2019-08-21 19:06:30      阅读:255      评论:0      收藏:0      [点我收藏+]

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) #查看cookie
print(r.headers) #查看请求头信息
# 当返回的响应格式是json时,可以用r.json())

# 这里有两种方式,一种是直接用请求地址,另一种是用参数的方式传递parames
url=‘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接口自动化

原文:https://www.cnblogs.com/qq0828/p/11380701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!