python 应用之 request 请求调用
场景: 验证一个第三方接口
目录
一、应用实例
1、预准备工作
1)、引用包
2)、生成随机串
3)、获得当前时间戳
4)、HASH
5)、header处理
6)、请求处理
2、requests请求
1)、常用用法
1.1)、get
1.2)、post
1.3)、返回的响应信息
2)、get请求 示例
3、源程序
一、应用实例
1、预准备工作
1)、引用包
因为还要做其它的工作,所以也引入了其它包
import requests
import json
import time
import hashlib
import codecs
import os
2)、生成随机串
生成32位随机串
def get_randstr():return codecs.encode(os.urandom(32),'hex').decode()
3)、获得当前时间戳
单位秒
def get_curTime():return int(time.time())
4)、HASH
def get_checksum(app_secret: str, nonce: str, timestamp: int):return hashlib.sha1(f'{app_secret}{nonce}{timestamp}'.encode()).hexdigest()
5)、header处理
def get_headers():sec="e"randStr=get_randstr()curTime=get_curTime()checkSum=get_checksum(sec,randStr,curTime)headers = {'Content-type': 'application/json','AppKey':'81b33512a','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum}return headers
6)、请求处理
#response1 = requests.get("",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="/"+str(channelID)+"/members"response1 = requests.get(url,headers=headers)return response1
2、requests请求
1)、常用用法
1.1)、get
requests.get(url,params,headers)
url:发送请求的链接。
params:携带的参数。
headers:头部信息。
1.2)、post
requests.post(url,data,headers)
url:发送请求的链接。
data:携带的json参数。
headers:头部信息。
1.3)、返回的响应信息
response.raise_for_status
如果返回的状态码不是200,通过此方法能够抛出异常。response.encoding
返回信息的编码格式。response.json()
获取返回回来的json数据。response.text
不是text() response.content
不是content()
2)、get请求 示例
headers = {'Content-type': 'application/json','AppKey':'81b3','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum
}mydata={'data':123
}
response1 = requests.get("",headers=headers,data=mydata)print(response1.json())
print(response1.text())
3、源程序
import requests
import json
import time
import hashlib
import codecs
import osdef get_randstr():return codecs.encode(os.urandom(32),'hex').decode()def get_curTime():return int(time.time())def get_checksum(app_secret: str, nonce: str, timestamp: int):return hashlib.sha1(f'{app_secret}{nonce}{timestamp}'.encode()).hexdigest()#response1 = requests.get("",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="/"+str(channelID)+"/members"response1 = requests.get(url,headers=headers)return response1def get_headers():sec="e"randStr=get_randstr()curTime=get_curTime()checkSum=get_checksum(sec,randStr,curTime)headers = {'Content-type': 'application/json','AppKey':'8','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum}return headersresponse1 =req_getRoomMember(134,get_headers())
print(response1.json())
print(response1.content)
# print(response1.request.headers)
#print(response1.request.body)
参考资料:
python爬虫之requests(附带四个入门案例)_requests 爬虫案例-CSDN博客
python 应用之 request 请求调用
场景: 验证一个第三方接口
目录
一、应用实例
1、预准备工作
1)、引用包
2)、生成随机串
3)、获得当前时间戳
4)、HASH
5)、header处理
6)、请求处理
2、requests请求
1)、常用用法
1.1)、get
1.2)、post
1.3)、返回的响应信息
2)、get请求 示例
3、源程序
一、应用实例
1、预准备工作
1)、引用包
因为还要做其它的工作,所以也引入了其它包
import requests
import json
import time
import hashlib
import codecs
import os
2)、生成随机串
生成32位随机串
def get_randstr():return codecs.encode(os.urandom(32),'hex').decode()
3)、获得当前时间戳
单位秒
def get_curTime():return int(time.time())
4)、HASH
def get_checksum(app_secret: str, nonce: str, timestamp: int):return hashlib.sha1(f'{app_secret}{nonce}{timestamp}'.encode()).hexdigest()
5)、header处理
def get_headers():sec="e"randStr=get_randstr()curTime=get_curTime()checkSum=get_checksum(sec,randStr,curTime)headers = {'Content-type': 'application/json','AppKey':'81b33512a','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum}return headers
6)、请求处理
#response1 = requests.get("",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="/"+str(channelID)+"/members"response1 = requests.get(url,headers=headers)return response1
2、requests请求
1)、常用用法
1.1)、get
requests.get(url,params,headers)
url:发送请求的链接。
params:携带的参数。
headers:头部信息。
1.2)、post
requests.post(url,data,headers)
url:发送请求的链接。
data:携带的json参数。
headers:头部信息。
1.3)、返回的响应信息
response.raise_for_status
如果返回的状态码不是200,通过此方法能够抛出异常。response.encoding
返回信息的编码格式。response.json()
获取返回回来的json数据。response.text
不是text() response.content
不是content()
2)、get请求 示例
headers = {'Content-type': 'application/json','AppKey':'81b3','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum
}mydata={'data':123
}
response1 = requests.get("",headers=headers,data=mydata)print(response1.json())
print(response1.text())
3、源程序
import requests
import json
import time
import hashlib
import codecs
import osdef get_randstr():return codecs.encode(os.urandom(32),'hex').decode()def get_curTime():return int(time.time())def get_checksum(app_secret: str, nonce: str, timestamp: int):return hashlib.sha1(f'{app_secret}{nonce}{timestamp}'.encode()).hexdigest()#response1 = requests.get("",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="/"+str(channelID)+"/members"response1 = requests.get(url,headers=headers)return response1def get_headers():sec="e"randStr=get_randstr()curTime=get_curTime()checkSum=get_checksum(sec,randStr,curTime)headers = {'Content-type': 'application/json','AppKey':'8','Nonce':randStr,'CurTime':str(curTime),'CheckSum':checkSum}return headersresponse1 =req_getRoomMember(134,get_headers())
print(response1.json())
print(response1.content)
# print(response1.request.headers)
#print(response1.request.body)
参考资料:
python爬虫之requests(附带四个入门案例)_requests 爬虫案例-CSDN博客