【代码实现接口测试】Requests库
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【代码实现接口测试】Requests库
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                Requests庫(kù)
Requests庫(kù)是用Python編寫(xiě)的,基于urllib,采用Apache2 Licensed開(kāi)源協(xié)議的HTTP庫(kù);
 相比與urllib庫(kù),Requests庫(kù)更加方便,可節(jié)約大量工作,完全滿足HTTP測(cè)試需求。
解決亂碼問(wèn)題
# 解決亂碼問(wèn)題: # 導(dǎo)包 import requests# 發(fā)送請(qǐng)求 response = requests.get("http://www.baidu.com")# 查看響應(yīng) # 查看響應(yīng)數(shù)據(jù)編碼格式 print("原始的數(shù)據(jù)編碼為:", response.encoding) print("設(shè)置前響應(yīng)數(shù)據(jù):", response.text)# 設(shè)置響應(yīng)數(shù)據(jù)編碼格式 response.encoding = "utf-8" print("設(shè)置編碼后數(shù)據(jù)編碼為:", response.encoding) print("設(shè)置好響應(yīng)數(shù)據(jù):", response.text)print(response.text)運(yùn)行結(jié)果展示:
 
 
發(fā)送url參數(shù)
# 導(dǎo)包 import requests# 發(fā)送請(qǐng)求 # 直接通過(guò)url傳遞參數(shù) response = requests.get("http://localhost:8080/Home/Goods/search.html?q=iPhone")# 通過(guò)params傳遞參數(shù): # (1)字符串 urlA = "http://localhost:8080/Home/Goods/search.html" stringA = "q=iPhone" response = requests.get(url=urlA,params=stringA)# (2)字典 dictA = {"q":"iPhone" } response = requests.get(url=urlA, params=dictA )# 查看響應(yīng) print(response.text)Post請(qǐng)求提交form表單數(shù)據(jù)
代碼:
""" 1.請(qǐng)求TPshop項(xiàng)目的登錄接口, 2.登錄接口URL: http://localhost:8080/index.php?m=Home&c=User&a=do_login """# 導(dǎo)包 import requests # 發(fā)請(qǐng)求 login_url = "http://localhost:8080/index.php?m=Home&c=User&a=do_login" login_data = {"username":"17788888888","password":"17788888888","verify_code":"8888" }responce = requests.post(url=login_url, data=login_data)# 看響應(yīng) print(responce.json())運(yùn)行結(jié)果:
 為什么格式正確它顯示驗(yàn)證碼錯(cuò)誤??
Post提交json數(shù)據(jù)
代碼如下:
# 導(dǎo)包 import requests # 發(fā)送請(qǐng)求 login_url = "http://ihrm-test.itheima.net/api/sys/login" login_data = {"mobile":"13800000002","password":"123456" }responce = requests.post(url=login_url, json=login_data)# 查看響應(yīng) print(responce.json())運(yùn)行結(jié)果如下:
 
其他請(qǐng)求類型
如PUT、DELETE、HEAD以及OPTIONS
import requestsresponse = requests.put("http://www.baidu.com", data={"key":"value"}) response = requests.delete("http://www.baidu.com")響應(yīng)內(nèi)容
| response.url | 請(qǐng)求url | 
| response.encoding | 查看響應(yīng)頭部字符編碼 | 
| response.headers | 頭信息 | 
| response.cookies | cookie信息 | 
| response.text | 文本形式的響應(yīng)內(nèi)容 | 
| response.content | 字節(jié)形式的響應(yīng)內(nèi)容 | 
| response.json() | JSON形式的響應(yīng)內(nèi)容 | 
代碼如下:
import requests# 訪問(wèn)百度首頁(yè)的接口`http://www.baidu.com`,獲取以下響應(yīng)數(shù)據(jù) response = requests.get("http://www.baidu.com")# 獲取響應(yīng)狀態(tài)碼 print("響應(yīng)狀態(tài)碼:", response.status_code)# 獲取請(qǐng)求URL print("URL:", response.url)# 獲取響應(yīng)字符編碼 print("編碼格式為:", response.encoding)# 獲取響應(yīng)頭數(shù)據(jù) print("響應(yīng)頭信息:", response.headers) print("Content-Type:",response.headers.get("Content-Type"))# 獲取響應(yīng)的cookie數(shù)據(jù) print("cookies:", response.cookies) print("提取指定的cookie", response.cookies.get("BDORZ"))# 獲取文本形式的響應(yīng)內(nèi)容 print("文本形式顯示響應(yīng)內(nèi)容:", response.text)# 獲取字節(jié)形式的響應(yīng)內(nèi)容 print("獲取字節(jié)形式的響應(yīng)內(nèi)容:", response.content) print("獲取字節(jié)形式的響應(yīng)內(nèi)容:", response.content.decode("utf-8"))運(yùn)行結(jié)果:
 
設(shè)置請(qǐng)求頭
""" 請(qǐng)求頭: Content-Type: application/json 請(qǐng)求體:{"mobile":"13800000002","password":"123456"} """import requests login_url = "http://ihrm-test.itheima.net/api/sys/login" login_header = {"Content-Type":"application/json" } login_data ={"mobile":"13800000002","password":"123456" }response = requests.post(url=login_url, json=login_data, headers=login_header)# 查看響應(yīng) print(response.json())運(yùn)行結(jié)果如下:
 
Session
session對(duì)象代表一次用戶會(huì)話:從客戶端瀏覽器連接服務(wù)器開(kāi)始,到客戶端瀏覽器與服務(wù)器斷開(kāi)會(huì)話能讓我們?cè)诳缯?qǐng)求時(shí)候保持某些參數(shù),比如在同一個(gè)session實(shí)例發(fā)出的所有請(qǐng)求之間保持cookie
import requests # 創(chuàng)建session對(duì)象 session = requests.Session()response = session.get("http://localhost:8080/index.php?m=Home&c=User&a=verify")# 登錄 login_url = "http://localhost:8080/index.php?m=Home&c=User&a=do_login" login_data = {"username":"17788888888","password":"17788888888","verify_code":"8888" } response = session.post(url=login_url, data=login_data) print(response.json())# http://localhost:8080/Home/Order/order_list.html response = session.get("http://localhost:8080/Home/Order/order_list.html") print(response.text)代碼運(yùn)行結(jié)果:
 
總結(jié)
以上是生活随笔為你收集整理的【代码实现接口测试】Requests库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 【数据库】pymysql数据库事务操作
- 下一篇: 苹果手机可以滚动截屏吗
