'''
timeout参数,设置请求的超时时间
1。接口执行耗时比较久,磨人的超时时间内执行不完,比如上传一个2G的大文件
2.接口性能要求,接口在xx ms内返回结果。超过xx ms,认为性能不符合要求
'''
import requests
# 淘宝查询手机号码归属地接口
# tcc.taobao.com/cc/json/mobile_tel_segment.htm接口地址
url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18012345678"
# 该接口是否能在200ms返回结果
for i in range(10):
try:
r = requests.get(url, timeout=0.5) # timeout=0.2 Connection to tcc.taobao.com timed out.时间太短了
# print(r.text)
print(r.status_code)
except Exception as e:
print(e)
'''
设置代理抓包
1.界面执行是正确的
2.接口执行时有问题
分别抓取界面执行过程的包,以及脚本执行过程的包,对比差异来定位问题
'''
proxy = {
"http": "http://127.0.0.1:8888", # 协议:代理地址
"https": "http://127.0.0.1:8888"
}
r = requests.get("http://wwwbaidu.com", proxies=proxy)
print(r.status_code)
# 证书校验失败:certificate verify failed,设置verify=False不去校验证书
# 执行时会出现警告,InsecureRequestWarning不影响执行
r = requests.get("https://www.bagevent.com",proxies=proxy,verify=False)
print(r.status_code)
|