一、什么是requests ?
requests是用python语言编写的简单易用的http库,用来做接口测试的库。
二、安装request库
1、在dos下输入pip install requests
2、在pycharm中seting中下载
3.查看是否安装
=====================================
查看方法:
=====================================
组建接口三种方式:
1、第一种
(1)requests.post
dx=requests.post( url =url ,data=date, json=json)di
案例:
import requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do" date1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.post(url=url1,data=date1,json=h1) print(dx.text) #以文本的形式显示响应码 print(dx.headers)#请求头 print(dx.cookies)#cookies值 print(dx.url)#接口路径 print(dx.status_code)#状态码 #200 print(dx.request) #请求方式 (2)requests.get 案例;
import requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456" h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.get(url=url1,params=h1) print(dx.text) #以文本的形式显示响应码 print(dx.headers)#请求头 print(dx.cookies)#cookies值 print(dx.url)#接口路径 print(dx.status_code)#状态码 #200 print(dx.request) #请求 第二种方法:
requests.request
(1)post请求
requests.request("post",url=url1,data=date1,json=h1) 案例:
import requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do" date1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.request("post",url=url1,data=date1,json=h1) print(dx.text) #以文本的形式显示响应码 print(dx.headers)#请求头 print(dx.cookies)#cookies值 print(dx.url)#接口路径 print(dx.status_code)#状态码 #200 print(dx.request) #请求方式 (2)get请求
mport requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456" h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.request("get",url=url1,params=h1) print(dx.text) #以文本的形式显示响应码 print(dx.headers)#请求头 print(dx.cookies)#cookies值 print(dx.url)#接口路径 print(dx.status_code)#状态码 #200 print(dx.request) #请求方式 第三种调用接口的方法: 使用session可以解决接口依赖关系 备注:如果没有依赖,就显示html页面格式 案例:
import requests s=requests.session()#创建一个s对象来保持上下接口的关联 class Cms(object): def __init__(self): pass def dl(self): url1="http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456" h1={"Content-Type":"application/x-www-form-urlencoded"} dx=s.get(url=url1,params=h1) print(dx.text) # 以文本的形式显示响应码 print(dx.headers) # 请求头 print(dx.cookies) # cookies值 print(dx.url) # 接口路径 print(dx.status_code) # 状态码 #200 print(dx.request) # 请求方式 def lmyh(self): url1 = "http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do?parentId=&categoryName=&page=1" h1 = {"Content-Type": "application/x-www-form-urlencoded"} dx = s.get(url=url1, params=h1) print(dx.text) # 以文本的形式显示响应码 print(dx.headers) # 请求头 print(dx.cookies) # cookies值 print(dx.url) # 接口路径 print(dx.status_code) # 状态码 #200 print(dx.request) # 请求方式 if __name__ == '__main__': c=Cms() c.dl() c.lmyh()
====================================== 依赖关系:通过cookies保持会话关系 案例:
import requests class Cms(object): def __init__(self): pass def dl(self): url1="http://cms.duoceshi.cn/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456" h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.get(url=url1,params=h1) print(dx.cookies) # cookies值 #<RequestsCookieJar[<Cookie JSESSIONID=BCAF009876267A5851EBA4DF9A47BA19 for cms.duoceshi.cn/cms>]> self.c=str(dx.cookies).split(" ")[1] print(self.c) print(dx.text) def lmyh(self): url1 = "http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do?parentId=&categoryName=&page=1" h1 = {"Content-Type": "application/x-www-form-urlencoded" ,'Cookie':self.c} dx = requests.get(url=url1,headers=h1) print(dx.text) # 以文本的形式显示响应码 print(dx.headers) # 请求头 print(dx.cookies) # cookies值 print(dx.url) # 接口路径 print(dx.status_code) # 状态码 #200 print(dx.request) # 请求方式 if __name__ == '__main__': c=Cms() c.dl() c.lmyh()
============================================== 断言: (1)if 语句
import requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do" date1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.post(url=url1,data=date1,json=h1) print(dx.text) #以文本的形式显示响应码 js=dx.json() print(js) if js["msg"]=="登录成功": print("ok") else: print("no") (2)
assert 断言
import requests url1="http://cms.duoceshi.cn/cms/manage/loginJump.do" date1={'userAccount':'admin','loginPwd':'123456'} h1={"Content-Type":"application/x-www-form-urlencoded"} dx=requests.post(url=url1,data=date1,json=h1) print(dx.text) #以文本的形式显示响应码 js=dx.json() print(js) assert js["msg"]=="登录成功!"