python自动化测试_3

接口测试异常处理:

发送https 请求:

HTTPS:
    url = 'https://www.juhe.cn/docs/api/id/39'
	r = requests.get(url)               #发送get请求
        

报错:

SSLError:证书问题:

1.faddler是否关闭,关闭后,访问成功:

2.请求参数后加上:verify=False  (不会校验证书)

body 部分:

Content-Type: application/x-www-form-urlencoded; charset=UTF-8:传data

Content-Type: application/json:传json

img

错误处理:

乱码:
	以content字节流输出,解码成utf-8:
    print(r.encoding)   # 查看返回的编码格式:

	print(r.content.decode('utf-8'))
warning 警告:
	import urllib3
	# 去掉警告
	urllib3.disable_warnings()
    
错误 403:
    拒绝或者禁止访问:须伪装头部(头部详情根据接口文档)
    
    更改User-Agent为浏览器
    
    伪装头部后仍然403:服务器校验Cookic(Cookic有时效性)
    
    头部加上Cookic访问

img

posted @ 2020-05-11 16:35  black__star  阅读(122)  评论(0编辑  收藏  举报