当前位置: 首页 > news >正文

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("https://logic-dev.netease.im/v2/api/rooms/1347405235210194/members",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="https://logic-dev.netease.im/v2/api/rooms/"+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("https://logic-dev.netease.im/v2/api/rooms/1347/members",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("https://logic-dev.netease.im/v2/api/rooms/1347405235210194/members",headers=headers)
def req_getRoomMember(channelID:int,headers:dict):url="https://logic-dev.netease.im/v2/api/rooms/"+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博客

http://www.lryc.cn/news/222758.html

相关文章:

  • BeanUtils.copyProperties浅拷贝的坑你得知道?
  • ubuntu安装rabbitMQ 并 开启记录消息的日志
  • 思维模型 首因效应
  • Redis极速上手开发手册【Redis全面复习】
  • [动态规划] (十四) 简单多状态 LeetCode LCR 091.粉刷房子
  • 【VSS版本控制工具】
  • 数据持久化技术(Python)的使用
  • 第23章(上)_索引原理之索引与约束
  • 金蝶云星空BOS设计器中基础资料字段属性“过滤”设置获取当前界面的基础资料值作为查询条件
  • OFDM深入学习及MATLAB仿真
  • 软件测试简历原来是写了这些才让面试官已读不回
  • ESP32网络开发实例-Web服务器RGB LED调光
  • C# TCP Server服务端多线程监听RFID读卡器客户端上传的读卡数据
  • 【electron】【附排查清单】记录一次逆向过程中,fetch无法请求http的疑难杂症(net::ERR_BLOCKED_BY_CLIENT)
  • 【JS】scrollTop+scrollHeight+clientTop+clientHeight+offsetTop+offsetHeight
  • Go语言函数用法
  • 3.5、Linux:命令行git的使用
  • 基于servlet+jsp+mysql网上书店系统
  • 自用工具类整理
  • jenkins2
  • YOLOv5独家改进:分层特征融合策略MSBlock | 南开大学提出YOLO-MS |超越YOLOv8与RTMDet,即插即用打破性能瓶颈
  • HTTP 协议详解-上(Fiddler 抓包演示)
  • 龙迅LT8911EXB功能概述 MIPICSI/DSI TO EDP
  • EtherCAT主站SOEM -- 5 -- SOEM之ethercatdc.h/c文件解析
  • 【分布式事务】深入探索 Seata 的四种分布式事务解决方案的原理,优缺点以及在微服务中的实现
  • C语言 || volatile
  • 网络安全之CSRF漏洞原理和实战,以及CSRF漏洞防护方法
  • vivo 网络端口安全建设技术实践
  • [ Linux Busybox ] flash_eraseall 命令解析
  • RabbitMQ 消息中间件 消息队列