requests---requests简介
在做接口測(cè)試的時(shí)候都會(huì)用到很多工具,如postman、jmeter、soupUI等工具,除了這些工具外,我們也可以用python的第3方庫(kù)requests來(lái)做接口測(cè)試。
?
request簡(jiǎn)介
requests是python實(shí)現(xiàn)的簡(jiǎn)單易用的HTTP庫(kù),屬于python的第3方庫(kù),通過(guò)pip進(jìn)行安裝使用。
requests中文文檔:https://2.python-requests.org//zh_CN/latest/user/quickstart.html#
requests安裝
1.打開(kāi)cmd
2.通過(guò)pip進(jìn)行安裝
# 安裝requests pip install requests?
requests發(fā)送get請(qǐng)求
源碼:
def get(url, params=None, **kwargs):r"""Sends a GET request.:param url: URL for the new :class:`Request` object.:param params: (optional) Dictionary, list of tuples or bytes to sendin the body of the :class:`Request`.:param \*\*kwargs: Optional arguments that ``request`` takes.:return: :class:`Response <Response>` object:rtype: requests.Response"""kwargs.setdefault('allow_redirects', True)return request('get', url, params=params, **kwargs)1.首先導(dǎo)入requests模塊
2.選擇get方法請(qǐng)求地址:https://www.cnblogs.com/qican/
3.可以查看請(qǐng)求的返回內(nèi)容
# coding:utf-8 # 導(dǎo)入模塊 import requests # 請(qǐng)求地址 url = 'https://www.cnblogs.com/qican/'r = requests.get(url) # 請(qǐng)求返回內(nèi)容 text = r.text print(text)
4.請(qǐng)求攜帶參數(shù)params
5.請(qǐng)求地址:http://httpbin.org/get?
6.請(qǐng)求參數(shù)書(shū)寫(xiě)以字典形式編寫(xiě)如{ "name":? "requests"?}
# coding:utf-8 # 導(dǎo)入requests模塊 import requests # 攜帶參數(shù) params = {"name": "request","name1":"python" } # 請(qǐng)求地址 url = 'http://httpbin.org/get?' r = requests.get(url,params=params) text = r.text print(text)代碼結(jié)果: {"args": {"name": "requests", "name1": "python"}, "headers": {"Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.21.0"}, "origin": "116.247.112.151, 116.247.112.151", "url": "https://httpbin.org/get?name=requests&name1=python" }
通過(guò)觀察,可以發(fā)現(xiàn)最后的url地址已經(jīng)被更改成了name=requests和name1=python
?
requests請(qǐng)求post
源碼:
def post(url, data=None, json=None, **kwargs):r"""Sends a POST request.:param url: URL for the new :class:`Request` object.:param data: (optional) Dictionary, list of tuples, bytes, or file-likeobject to send in the body of the :class:`Request`.:param json: (optional) json data to send in the body of the :class:`Request`.:param \*\*kwargs: Optional arguments that ``request`` takes.:return: :class:`Response <Response>` object:rtype: requests.Response"""return request('post', url, data=data, json=json, **kwargs)1.導(dǎo)入requests模塊
2.選擇post方法請(qǐng)求:http://apis.juhe.cn/simpleWeather/query
3.輸入?yún)?shù)格式{”name“:”value“}
# coding:utf-8 import requests # 導(dǎo)入模塊 url = 'http://apis.juhe.cn/simpleWeather/query' # 請(qǐng)求地址 # 請(qǐng)求參數(shù) data = {"city":"上海","key":"331eab8f3481f37868378fcdc76cb7cd" } r = requests.post(data=data,url=url) print(r.text)?
返回值其他內(nèi)容
r.text # 返回全部?jī)?nèi)容 r.url # 返回的url地址 r.content # 返回解碼后的內(nèi)容 r.cookies # 返回cookies r.headers # 返回?cái)y帶的請(qǐng)求頭 r.status_code # 返回狀態(tài)碼 r.json() # 返回json格式?
轉(zhuǎn)載于:https://www.cnblogs.com/qican/p/11153054.html
總結(jié)
以上是生活随笔為你收集整理的requests---requests简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 算法两数相加
- 下一篇: 顺序表的结构和9个基本运算算法