grequest案例对比requests案例
2024-10-21 10:25:11
grequets和requests案例各一个,使用上对比:
import grequests
import requests headers = {
"content-type": "application/json;charset=UTF-8",
"authentication": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
".eyJpc3MiOiJwYXNzcG9ydHRlc3Quc2VuZ3VvLm1lIiwiZXhwIjoxNjA5MjI4MzczLCJwYXNzcG9ydF9pZCI6MzI4OTg4MCwic3ViIjoiYXV0aF90b2tlbiIsImF1ZCI6ImxpbmdzaG91IiwiaWF0IjoxNjA4NjIzNTczfQ.UiKxI2gtDy9vl43F_i4305KaGeADAyuTpJxSwtXqAfY",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/72.0.3626.121 Safari/ "
}
url = 'https://cashiertest.sg.me/api/finance/getonedayshopprofit'
data = '{"date": "2020-12-22"}' def g_request():
res = [grequests.post(url=url, data=data, headers=headers)]
rp = grequests.map(res)
for i in rp:
print(i.text) def requests_():
res = requests.post(url=url, data=data, headers=headers)
print(res.text) if __name__ == '__main__':
g_request()
requests_() 控制台输出都一样:
{"message":"请求成功","data":{"shop_profit":{"is_confirmed":false,"gross_profit":null,"gross_profit_rate":null},"shop_goods_profits":[]}}
{"message":"请求成功","data":{"shop_profit":{"is_confirmed":false,"gross_profit":null,"gross_profit_rate":null},"shop_goods_profits":[]}}
grequest标准使用:
import grequests # gevent+request request_list = [
grequests.get('https://www.runoob.com/python3/python3-file-methods.html', timeout=0.1),
grequests.get('https://www.zxgj.cn/g/tupiancaijian'),
grequests.get('https://www.json.cn/')
]
# 响应列表
response_list = grequests.map(request_list)
# 取响应码(和request用于)
response_content = response_list[0].status_code
# 取json
# ips = [ret.json() for ret in response_list if ret and ret.status_code == 200]
print(response_content)
最新文章
- ASP.NET MVC Html.BeginForm 设置 timeout
- $Host.Runspace.ThreadOptions = “ReuseThread”有神马用?
- nios II--实验4——按键中断软件部分
- Python中判断是否为闰年,求输入日期是该年第几天
- Delphi中的变量作用域简介
- paypal IPN 接口返回INVALID参数可能问题
- CDC
- HDU2176尼姆博弈
- 字符编解码的故事(ASCII,ANSI,Unicode,Utf-8)
- hdoj 2544 最短路【dijkstra or spfa】
- 【转】Android HAL实例解析
- 【Chromium中文文档】Chrome/Chromium沙箱 - 安全架构设计
- IIS的安装
- 利用 onload 事件监控跨站资源
- [LeetCode66]Plus One
- Redis入门到高可用(四)—— Redis的五种数据结构的内部编码
- 【原创】Qt 使用ODBC driver 连接SQL Server
- idea中maven依赖不能下载的解决办法
- Spring AOP详解(转载)
- dip,px,sp区别及使用场景