美文网首页
Python 如何获取接口返回的http状态码

Python 如何获取接口返回的http状态码

作者: Queenie的学习笔记 | 来源:发表于2019-10-15 11:24 被阅读0次

使用request 模块得到返回值,然后再利用status_code属性获取http响应的状态码。

示例代码:

import requests
import json

url = 'http://dev.xxx.com/loginAccount'
data = {"account": "companyG", "adminUserCode": "000009", "companyCode": "53c3aeab8c1e11e9898400163e138319", "password": "c4ca4238a0b923820dcc509a6f75849b", "timestamp": "20191009112205", "sign": "55efce47806e2056adfa0cb769c274c8"} 
header = {"Content-Type":"application/json"}

res = requests.post(url,json=data,headers=header) #这里的语法参见 https://blog.csdn.net/weixin_43709411/article/details/91046552

# 3.解析响应:输出响应文本等等
#print(res.text)  # 输出响应的文本
print("接口1状态:",res.status_code)  # 状态码
#print(res.headers)  # 响应头


url2 = 'https://dev.xxx.com/show?t=1571107362548&token=3c55b539bd8d0a31f7f673ad6334235daa9c2cf5'
res2 = requests.get(url2)
print("接口2状态:",res2.status_code)

返回结果:


返回结果

相关文章

网友评论

      本文标题:Python 如何获取接口返回的http状态码

      本文链接:https://www.haomeiwen.com/subject/pinjmctx.html