python3接口测试(requests库),,一、一般概念1.导入
python3接口测试(requests库),,一、一般概念1.导入
一、一般概念
1.导入第三方库
import requests
2.发送get请求
#userURL为客户端访问的URL地址
myResponse = requests.get(userURL)
3.查看返回结果
#myResponse.header包含内容:{‘Server‘: ‘nginx/1.10.1‘, ‘Date‘: ‘Sat, 18 Aug 2018 02:57:28 GMT‘, ‘Content-Type‘: ‘text/html; charset=GBK‘, ‘X-Powered-By‘: ‘PHP/7.0.10‘, ‘Content-Encoding‘: ‘gzip‘, ‘Age‘: ‘0‘, ‘Transfer-Encoding‘: ‘chunked‘, ‘Connection‘: ‘keep-alive‘, ‘Via‘: ‘http/1.1 swg.com ("SKG-UCSG")‘}
4.requests函数有几个典型方法
requests.request()
requests.get()
requests.post()
requests.put()
requests.delete()
requests.head()
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二、应用
要求:
1.请求的URL是https,可能会报SSL认证错误;
2.对请求头(request head)中的元素赋值,例如content-type。
3.通过代理(proxies)发送请求。
import requests#访问https时会报证书错误((Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] ),可以在发送请求时不验证。#屏蔽waring信息#requests.packages.urllib3.disable_warnings()#一般的get请求方法#myrequest = requests.get(r"https://www.baidu.com",verify=False)#print (myrequest.headers)#准备请求数据myUrl = r"https://www.cnblogs.com/mpp0905/p/9264465.html"myHeader = {"content-type":"image/jpeg"}#代理myProxies = {‘http‘ : ‘http://url:port‘}#发送请求myRequest = requests.request("GET",myUrl,headers=myHeader,proxies=myProxy,verify=False)#当返回页面中有中文时,需要对返回页面进行编码myRequest.encoding = ‘utf8‘#判断返回页面print (myRequest.status_code)assert u"页面中应出现的元素" in (myRequest.text),u‘不是提示界面。‘
python3接口测试(requests库)
相关内容
- 1.16 Python基础知识 - 装饰器,, Python中的
- Python切片操作,,在利用Python解
- NO.1:自学python之路,,引言人工智能如今越来
- python 文件操作(三),,转自 http://
- Python随心记--线程列队,,import thr
- 3分钟学会,学会用Python正确读取大文件,,文件读写属
- Java-Python对垒之质数计算,,@目录Java 实现
- conda create -n AlphaPose2018 python=2.7,,conda crea
- 如何在MacOS的VScode上安装Python3,,由于MacOS上的V
- 离线下载vs code 插件 python源码,, 1 import
评论关闭