python requests函数封装方法
python requests函数封装方法
上代码
import requests
import json """
封装request请求,
1.post:my_post
2.get:my_get
3.返回code:get_code(res)
4.返回json:get_json(res)
5.返回text:get_text(res)
6.响应时间:get_time(res)
7.请求header:get_header(act)
9.添加请求头参数:add_header(dict) """ #---------------------------------------------------
"""
r.status_code
r.text #页面内容
r.encoding #header中的编码方式
r.apparent_encoding #备选的编码方式
r.content #响应内容的二进制形式
timeout=
r.elapsed.total_seconds(),单位是s
"""
#---------------------------------------------------- def my_post(url,payload,headers,timeout=30):
res = requests.request("POST", url, data=payload, headers=headers,timeout=timeout)
return res def my_get(url,payload,headers,querystring,timeout=30):
resp = requests.request("GET", url, data=payload, headers=headers, params=querystring,timeout=timeout)
#获取返回code
code=res.status_code
print('code',code)
return res def get_code(res):
#获取返回code
code=res.status_code
print('code:\n',code) def get_json(res):
#获取返回json
print('res.json:\n',res.json())
return res.json() def get_text(res):
print('res.text:\n',res.text)
return res.text def get_time(res):
#获取响应执行时间,单位s
time=res.elapsed.total_seconds()
print('res.time:\n',res.elapsed.total_seconds())
return time def get_header(act):
if act=="json": json_header={
'content-type': "application/json",
}
return json_header
else:
str_header={
'content-type': "application/x-www-form-urlencoded",
}
return str_header def add_header(dict):
headers=get_header("json")
for k,v in dict.items():
headers[k]=v
return headers if __name__=="__main__": url="http://192.168.0.10:3080/asg/portal/call/231.do" #json转换格式
strData={"pub":{"deviceId":"dz630761d39e7145a3850eedc4563e61ff","subPline":"","screen":"1080x1920","appCode":"f002","dzPaySupport":"","userId":"","city":"%E5%8C%97%E4%BA%AC","utdid":"WVXnflOMWeEDAG79IwDB2QuM","apiVersion":"3.9.7.3004","province":"%E5%8C%97%E4%BA%AC%E5%B8%82","v":"","afu":"","imei":"","p":"","clientAgent":"svnVer_1907171924","lsw":"","apn":"wifi","imsi":"","channelFee":"Google","cmTel":"","sign":"1ea70e2fc19f5da6f4bc926c35962559","pname":"com.ishugui","channelCode":"Google","os":"android23","brand":"Xiaomi","en":"{\"adsdk\":\"1\"}","macAddr":"AC:C1:EE:F8:D1:F6","model":"Redmi Note 4X"},"pri":{"v":"","idStrs":"11000007217:25186137","sign_data":1,"is_sdk":"","last_rcmbook_id":"","installedFreeApk":0,"index":3,"f":"f0,f1,f2,f3,f4,f5,f6,f7","sex":2,"vtv":""}}
strJson=json.dumps(strData)
print('strJson----- ',strJson) timeout=30
headers=get_header("json") res=my_post(url,strJson,headers,timeout) get_code(res)
get_json(res)
get_text(res)
python requests函数封装方法的更多相关文章
- python基础函数、方法
python的函数和方法,通过def 定义: 函数的特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数和方法的区别:函数有返回值.方法没有 语法定义: def sayhi():#函数名 p ...
- Python列表函数和方法
Python列表函数和方法: 函数: len(列表名): 返回列表长度 # len(列表名): # 返回列表长度 lst = [1,2,3,'a','b','c'] print("lst 列 ...
- python(函数封装)
一:Python 自定义函数 函数示意图如下: 1.使用函数的好处: 代码重用 保持一致性,易维护 可扩展性 2.函数定义 函数定义的简单规则: 函数代码块以def关键词开头 后接函数标识符名称和圆括 ...
- python常用函数和方法 - 备忘
语法语句篇 除法运算(精确运算和截断运算) 在python2中,除法运算通常是截断除法.什么是截断除法: >>> 3/4 0 # 自动忽略小数项 要是想 得到正确结果 怎么办呢? m ...
- python中函数与方法的区别
在python中,其实函数和方法的区别取决于其调用者,在普通的函数定义中就叫做函数 例如: def func(): print('这是一个函数') 而在一个类中定义时,就将其分为两种情况 第一种:被称 ...
- Python | Python常用函数、方法示例总结(API)
目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...
- Python中函数和方法的区别
方法是一种特殊的函数属于某个类的的函数叫方法不属于某个类的函数叫函数 转自csdn https://blog.csdn.net/weixin_40380298/article/details/7825 ...
- Python 中函数和方法
函数与方法 class Foo(object): def __init__(self): self.name = 'lcg' def func(self): print(self.name) obj ...
- Python常用函数、方法、模块记录
常用函数: 1.pow():乘方 2.abs():绝对值 3.round():四舍五入 4.int():转换为整数 5.input():键盘输入(会根据用户的输入来做类型的转换) raw_input( ...
随机推荐
- 6105 - deauth after EAPOL key exchange sequence
wifi无法连接公司的网络 Warning Error in Event Log - deauth after EAPOL key exchange sequence https://forums.i ...
- 杂项-报表-Minitab:Minitab百科
ylbtech-杂项-报表-Minitab:Minitab百科 Minitab软件是现代质量管理统计的领先者,全球六西格玛实施的共同语言,以无可比拟的强大功能和简易的可视化操作深受广大质量学者和统计专 ...
- Map-Amap:货运解决方案
ylbtech-Map-Amap:货运解决方案 1.返回顶部 1. http://lbs.amap.com/smart/truck/ 2. 2.返回顶部 1. 2. 3.返回顶部 4.返回顶部 ...
- 用 Flask 来写个轻博客 (31) — 使用 Flask-Admin 实现 FileSystem 管理
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 编写 FileSystem Admin 页面 Flask-A ...
- PAT甲级【2019年9月考题】——A1163 PostfixExpression【25】
7-3 Postfix Expression (25 分) Given a syntax tree (binary), you are supposed to output the correspon ...
- asp.net dev xtraReporting(一)静态页面
新建一个空web应用程序 添加一个default.aspx 添加新建项 建一个空report 写点数据 defalut.aspx中拖拽一个aspxDocumentView 预览可以实现展示了
- W3C、MDN及html常用标签介绍
W3C 万维网(World Wide Web)是作为欧洲核子研究组织的一个项目发展起来的,在那里 Tim Berners-Lee 开发出万维网的雏形.Tim Berners-Lee- 万维网的发明人和 ...
- 超強的Linux指令解釋網站《explainshell.com》,學Linux必備!
ExplainShell 官方網站:http://explainshell.com/ 原始碼下載:https://github.com/idank/explainshell 用瀏覽器打該explain ...
- jQuery对于demo元素的上移、下移、删除操作等实现
今天给大家分享一个实用的jQuery技能:dom元素的操作:我们经常会去获取dom元素去实现交互效果,以及数据的操作. 首先复习一下jQuery DOM 元素方法: .get() 获得由选择器指定的D ...
- vscode 常用命令行
Ctrl+Shift+P: 打开命令面板 打开一个新窗口: Ctrl+Shift+N 关闭窗口: Ctrl+Shift+W 新建文件 Ctrl+N 代码行缩进 Ctrl+[ . Ctrl+] 上下 ...