Python(request库)

server/2025/3/27 22:14:20/

request库 

一、request库介绍

(1)简介

requests是用python语言编写的简单易用的http库,用来做接口测试的库


(2)接口测试自动化库有哪些?

 requests、urllib 、urllib2、urllib3、 httplib 等(最受欢迎的是requests) 

(3)安装request库

方式一:

dos下pip: 命令:pip install requests

图片

方法二:

pycharm 中 setting下载

图片

 (4)组建一个接口需要参数?


a、url  (域名+端口+接口路径)
c、请求头 (请求类型heards)
d、请求参数(请求体:账号,密码)


二、requests中的运行


1、导入requests 模块

2、运行的三种方法:

(1)第一种方法:


requests.post(url=url,data=data,json=heardes)

图片

ctrl  鼠标点击  post查看方法

案例:
import   requests
url1="http://49.233.201.254:8080/cms/manage/loginJump.do"
data1={'userAccount':'admin','loginPwd':'123456'}
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=requests.post(url=url1,data=data1,json=h1)
print(jk1.text) #打印响应体
print(jk1.json()) #响应体转换成json格式
print(jk1.cookies) #打印cookie
print(jk1.status_code)# 打印状态码
print(jk1.request) #打印接口请求
print(jk1.url) #打印url
第一种方法中的get请求:
import   requests
url1="http://49.233.201.254:8080/cms/manage/loginJump.do"
data1={'userAccount':'admin','loginPwd':'123456'}
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=requests.get(url=url1,params=data1,headers=h1)
print(jk1.text) #打印响应体
print(jk1.json()) #响应体转换成json格式
print(jk1.cookies) #打印cookie
print(jk1.status_code)# 打印状态码
print(jk1.request) #打印接口请求
print(jk1.url) #打印url

图片

图片

get参数接在url后面
import   requests
url1="http://49.233.201.254:8080/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456"
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=requests.get(url=url1,headers=h1)
print(jk1.text) #打印响应体
print(jk1.json()) #响应体转换成json格式
print(jk1.cookies) #打印cookie
print(jk1.status_code)# 打印状态码
print(jk1.request) #打印接口请求
print(jk1.url) #打印url
(2)第二种调用方法:
requests.request(请求方法,url,headers=h1)

图片

案例1:
requests.request(get,url,headers=h1) 方法
 
import   requests
url1="http://49.233.201.254:8080/cms/manage/loginJump.do?userAccount=admin&loginPwd=123456"
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=requests.request("get",url1,headers=h1)
print(jk1.text) #打印响应体
print(jk1.json()) #响应体转换成json格式
print(jk1.cookies) #打印cookie
print(jk1.status_code)# 打印状态码
print(jk1.request) #打印接口请求
print(jk1.url) #打印url
案例2:
requests.request(post,url,data,headers=h1) 方法
import   requests
url1="http://49.233.201.254:8080/cms/manage/loginJump.do"
data1={'userAccount':'admin','loginPwd':'123456'}
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=requests.request("post",url=url1,data=data1,headers=h1)
print(jk1.text) #打印响应体
print(jk1.json()) #响应体转换成json格式
print(jk1.cookies) #打印cookie
print(jk1.status_code)# 打印状态码
print(jk1.request) #打印接口请求
print(jk1.url) #打印url
 
(3)第三种 方法:保持会话关系

requests.Session()调用方法

import   requests
s=requests.Session()
url1="http://49.233.201.254:8080/cms/manage/loginJump.do"
data1={'userAccount':'admin','loginPwd':'123456'}
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=s.request("post",url=url1,data=data1,headers=h1)
print(jk1.text) #打印响应体
#查询用户接口
url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"
data2={
'startCreateDat':'',
'endCreateDate':'',
'searchValue':'',
'page':'1'
}
h2={"Content-Type":"application/x-www-form-urlencoded"}
jk2=s.post(url=url2,data=data2,headers=h2)
print(jk2.text)

 


request断言

一、if断言

案例:

import   requests
s=requests.Session()
url1="http://49.233.201.254:8080/cms/manage/loginJump.do"
data1={'userAccount':'admin','loginPwd':'123456'}
h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=s.request("post",url=url1,data=data1,headers=h1)
wb=jk1.json()
print(jk1.json())
if wb["msg"]=="登录成功!":print("ok")
else:print("no")

二.assert断言

h1={"Content-Type":"application/x-www-form-urlencoded"}
jk1=s.request("post",url=url1,data=data1,headers=h1)
wb=jk1.json()
print(jk1.json())
assert  wb["msg"]=='登录成功'
print(jk1.cookies)

图片


封装接口

案例1:requests.Session()

import   requests
s=requests.Session()
class  Cms(object):def __init__(self):passdef  dl(self):url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do"data1={'userAccount':'admin','loginPwd':'123456'}h1={"Content-Type":"application/x-www-form-urlencoded"}jk1=s.request("post",url=url1,data=data1,headers=h1)print(jk1.json())def   yh(self):url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"data2={'startCreateDat':'','endCreateDate':'','searchValue':'','page':'1'}h2={"Content-Type":"application/x-www-form-urlencoded"}jk2=s.request("post",url=url2,data=data2,headers=h2)print(jk2.text)
if __name__ == '__main__':dx=Cms()dx.dl()dx.yh()

案例2:cookies 保持会话

图片

图片

代码:

import   requests
class  Cms(object):def __init__(self):passdef  dl(self):url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do"data1={'userAccount':'admin','loginPwd':'123456'}h1={"Content-Type":"application/x-www-form-urlencoded"}jk1=requests.request("post",url=url1,data=data1,headers=h1)print(jk1.text)c=str(jk1.cookies)print(c) #<RequestsCookieJar[<Cookie JSESSIONID=DAB3F94092C907F3BAD009410A8778E0 for 49.233.201.254/cms/>]>self.cookie=c.split(" ")[1] #JSESSIONID=6D60B90E937058D8B91B66A18671A4CB# print(cookie)def   yh(self):url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"data2={'startCreateDat':'','endCreateDate':'','searchValue':'','page':'1'}h2={"Content-Type":"application/x-www-form-urlencoded","Cookie":self.cookie}jk2=requests.request("post",url=url2,data=data2,headers=h2)print(jk2.text)
if __name__ == '__main__':dx=Cms()dx.dl()dx.yh()

案例3:

import   requests
class  Cms(object):def __init__(self):passdef  dl(self):url1 = "http://49.233.201.254:8080/cms/manage/loginJump.do"data1={'userAccount':'admin','loginPwd':'123456'}h1={"Content-Type":"application/x-www-form-urlencoded"}jk1=requests.request("post",url=url1,data=data1,headers=h1)print(jk1.text)c=str(jk1.cookies)print(c) #<RequestsCookieJar[<Cookie JSESSIONID=DAB3F94092C907F3BAD009410A8778E0 for 49.233.201.254/cms/>]>cookie=c.split(" ")[1] #JSESSIONID=6D60B90E937058D8B91B66A18671A4CB# print(cookie)return cookiedef   yh(self):d=self.dl()url2="http://49.233.201.254:8080/cms/manage/queryUserList.do"data2={'startCreateDat':'','endCreateDate':'','searchValue':'','page':'1'}h2={"Content-Type":"application/x-www-form-urlencoded","Cookie":d}jk2=requests.request("post",url=url2,data=data2,headers=h2)print(jk2.text)
if __name__ == '__main__':dx=Cms()# dx.dl()dx.yh()

图片


关联接口

省份接口:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince

城市接口:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupport

入参:CitybyProvinceName:{{cs}}

案例1:


import   requests s=requests.Session() class  Gl(object):     def __init__(self):         pass     def  sf(self):         url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"         jk=s.post(url=url)         print(jk.text)     def  cs(self):         url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"         data1={'byProvinceName':"浙江"}         h1={"Content-Type":"application/x-www-form-urlencoded"}         jk1=s.post(url=url1,data=data1,)         print(jk1.text) if __name__ == '__main__':     dx=Gl()     dx.sf()     dx.cs()

案例2:

图片

import   requests
import  re
s=requests.Session()
class  Gl(object):def __init__(self):passdef  sf(self):url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportProvince"jk=s.post(url=url)print(jk.text)self.tq=re.findall('<string>(.+?)</string>',jk.text)# print(tq)def  cs(self):url1=r"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity"data1={'byProvinceName':self.tq[8]}h1={"Content-Type":"application/x-www-form-urlencoded"}jk1=s.post(url=url1,data=data1,headers=h1)print(jk1.text)
if __name__ == '__main__':dx=Gl()dx.sf()dx.cs()

http://www.ppmy.cn/server/179149.html

相关文章

git的进阶使用

一.协作冲突 举个简单的例子&#xff0c;公司里两个人(A,B)同一天上班&#xff0c;都拉取了远程仓库数据。然后A做完了所有的工作&#xff0c;进行了x文件的修改并提交至远程仓库。而B在做自己工作的时候不小心或者需要修改x文件&#xff0c;B认为A没有操作x文件直接push没有问…

Webview详解(上)

第一阶段&#xff1a;基础入门 WebView基础概念 什么是Webview&#xff1f; WebView是一种用于在移动应用程序中展示网页内容的嵌入式浏览器组件。它允许开发者将网页内容直接加载到应用界面中&#xff0c;用户无需离开应用即可浏览网页。WebView 通常用于加载 HTML、CSS、J…

【大模型算法工程】大模型应用工具化、忠诚度以及知识库场景下PDF双栏解析问题的讨论

1. 大模型时代应用工具化以及无忠诚度现象讨论 接触大模型久了&#xff0c;也慢慢探到一些大模型能力表现非常自然和突出的场景&#xff0c;比如AI搜索&#xff08;依赖大模型的理解总结能力&#xff09;、AI对话&#xff08;即chat&#xff0c;依赖大模型的生成能力&#xff0…

深入解析 Service Worker 在 Chrome 扩展中的应用

1. 什么是 Service Worker&#xff1f; Service Worker 是一种运行在后台的 JavaScript 线程&#xff0c;与网页主线程独立。它的主要作用是拦截网络请求、缓存资源、提供离线支持&#xff0c;并执行后台任务&#xff0c;如推送通知和后台同步。在 Chrome 扩展&#xff08;Ext…

leetcode-45.跳跃游戏II

题很简单&#xff0c;怎么少点写代码比较困难。 这题很明显贪心算法&#xff0c;每次跳跃我们都希望理论上能到达的地方更远。一开始站在初始点&#xff0c;那么可选范围就是0到nums[0]&#xff0c;在这里面找能跳得最远的点i&#xff0c;接下来遍历nums[0]1到 inums[i]&#x…

CityEngine:3D城市建模专家

今天为大家介绍的软件是CityEngine&#xff1a;3D城市建模专家, 这款建模软件也是ESRI的产品. 下面我们将从软件的主要功能、支持的系统、软件官网等方面对其进行简单的介绍。 CityEngine的官网网址为https://www.esri.com/zh-cn/arcgis/products/arcgis-cityengine/overview。…

Flink 自定义数据源:从理论到实践的全方位指南

目录 第一章:自定义数据源的基础概念 数据源是什么?它在 Flink 中扮演什么角色? Flink 的内置数据源:开箱即用的 “标配” 为什么需要自定义数据源?它的杀手锏在哪? 第二章:自定义数据源的实现之道 接口选择:从简单到高级,选对工具事半功倍 SourceFunction:入门…

当今前沿科技:改变世界的最新技术趋势

人工智能AI&#xff09;在医疗影像诊断中越来越重要。它帮助医生分析影像&#xff0c;提高了诊断的准确性和效率。传统的方法依赖于医生的经验&#xff0c;可能会出现误诊或漏诊。AI能快速处理大量影像数据&#xff0c;减少这些问题。 AI通过深度学习和图像识别技术&#xff0…