【代码实现接口测试】Requests库
生活随笔
收集整理的這篇文章主要介紹了
【代码实现接口测试】Requests库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Requests庫
Requests庫是用Python編寫的,基于urllib,采用Apache2 Licensed開源協議的HTTP庫;
相比與urllib庫,Requests庫更加方便,可節約大量工作,完全滿足HTTP測試需求。
解決亂碼問題
# 解決亂碼問題: # 導包 import requests# 發送請求 response = requests.get("http://www.baidu.com")# 查看響應 # 查看響應數據編碼格式 print("原始的數據編碼為:", response.encoding) print("設置前響應數據:", response.text)# 設置響應數據編碼格式 response.encoding = "utf-8" print("設置編碼后數據編碼為:", response.encoding) print("設置好響應數據:", response.text)print(response.text)運行結果展示:
發送url參數
# 導包 import requests# 發送請求 # 直接通過url傳遞參數 response = requests.get("http://localhost:8080/Home/Goods/search.html?q=iPhone")# 通過params傳遞參數: # (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 )# 查看響應 print(response.text)Post請求提交form表單數據
代碼:
""" 1.請求TPshop項目的登錄接口, 2.登錄接口URL: http://localhost:8080/index.php?m=Home&c=User&a=do_login """# 導包 import requests # 發請求 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)# 看響應 print(responce.json())運行結果:
為什么格式正確它顯示驗證碼錯誤??
Post提交json數據
代碼如下:
# 導包 import requests # 發送請求 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)# 查看響應 print(responce.json())運行結果如下:
其他請求類型
如PUT、DELETE、HEAD以及OPTIONS
import requestsresponse = requests.put("http://www.baidu.com", data={"key":"value"}) response = requests.delete("http://www.baidu.com")響應內容
| response.url | 請求url |
| response.encoding | 查看響應頭部字符編碼 |
| response.headers | 頭信息 |
| response.cookies | cookie信息 |
| response.text | 文本形式的響應內容 |
| response.content | 字節形式的響應內容 |
| response.json() | JSON形式的響應內容 |
代碼如下:
import requests# 訪問百度首頁的接口`http://www.baidu.com`,獲取以下響應數據 response = requests.get("http://www.baidu.com")# 獲取響應狀態碼 print("響應狀態碼:", response.status_code)# 獲取請求URL print("URL:", response.url)# 獲取響應字符編碼 print("編碼格式為:", response.encoding)# 獲取響應頭數據 print("響應頭信息:", response.headers) print("Content-Type:",response.headers.get("Content-Type"))# 獲取響應的cookie數據 print("cookies:", response.cookies) print("提取指定的cookie", response.cookies.get("BDORZ"))# 獲取文本形式的響應內容 print("文本形式顯示響應內容:", response.text)# 獲取字節形式的響應內容 print("獲取字節形式的響應內容:", response.content) print("獲取字節形式的響應內容:", response.content.decode("utf-8"))運行結果:
設置請求頭
""" 請求頭: Content-Type: application/json 請求體:{"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)# 查看響應 print(response.json())運行結果如下:
Session
session對象代表一次用戶會話:從客戶端瀏覽器連接服務器開始,到客戶端瀏覽器與服務器斷開會話能讓我們在跨請求時候保持某些參數,比如在同一個session實例發出的所有請求之間保持cookie
import requests # 創建session對象 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)代碼運行結果:
總結
以上是生活随笔為你收集整理的【代码实现接口测试】Requests库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据库】pymysql数据库事务操作
- 下一篇: 运维经验 清空大量日志