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函数封装方法的更多相关文章

  1. python基础函数、方法

    python的函数和方法,通过def 定义: 函数的特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数和方法的区别:函数有返回值.方法没有 语法定义: def sayhi():#函数名 p ...

  2. Python列表函数和方法

    Python列表函数和方法: 函数: len(列表名): 返回列表长度 # len(列表名): # 返回列表长度 lst = [1,2,3,'a','b','c'] print("lst 列 ...

  3. python(函数封装)

    一:Python 自定义函数 函数示意图如下: 1.使用函数的好处: 代码重用 保持一致性,易维护 可扩展性 2.函数定义 函数定义的简单规则: 函数代码块以def关键词开头 后接函数标识符名称和圆括 ...

  4. python常用函数和方法 - 备忘

    语法语句篇 除法运算(精确运算和截断运算) 在python2中,除法运算通常是截断除法.什么是截断除法: >>> 3/4 0 # 自动忽略小数项 要是想 得到正确结果 怎么办呢? m ...

  5. python中函数与方法的区别

    在python中,其实函数和方法的区别取决于其调用者,在普通的函数定义中就叫做函数 例如: def func(): print('这是一个函数') 而在一个类中定义时,就将其分为两种情况 第一种:被称 ...

  6. Python | Python常用函数、方法示例总结(API)

    目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...

  7. Python中函数和方法的区别

    方法是一种特殊的函数属于某个类的的函数叫方法不属于某个类的函数叫函数 转自csdn https://blog.csdn.net/weixin_40380298/article/details/7825 ...

  8. Python 中函数和方法

    函数与方法 class Foo(object): def __init__(self): self.name = 'lcg' def func(self): print(self.name) obj ...

  9. Python常用函数、方法、模块记录

    常用函数: 1.pow():乘方 2.abs():绝对值 3.round():四舍五入 4.int():转换为整数 5.input():键盘输入(会根据用户的输入来做类型的转换) raw_input( ...

随机推荐

  1. 关于css3 Animation动画

    在介绍animation之前有必要先来了解一个东西,那就是“keyframes”,我们把他叫做“关键帧”: 在使用transition制作一个简单的transition效果时,包括了初始属性,最终属性 ...

  2. java的实用类

    1)   Random类 用于生成随机数字,所有生成的数字,都是等概率的. nextInt():生成的值介于int的所有取值范围(-231 ~ 231-1) nextInt(int value):生成 ...

  3. shell编程:sed的选项

    sed [参数] [partern/commond] file 标准输出 | sed sed [参数] [partern/commond] -n :使用安静(silent)模式.在一般 sed 的用法 ...

  4. 【目录】Identityserver 4 老张的哲学

    随笔分类 - .IdentityServer4 从壹开始 [ Ids4实战 ] 之四 ║ 用户数据管理 & 前后端授权联调 摘要: 前言 哈喽~~~ 大家周一好!夏天到了,大家舒服了没有,熟话 ...

  5. 定时任务crond介绍

    定时任务cornd crond介绍: crond是linux系统中用来定期(或周期性)执行命令或指定程序任务脚本的一种程序. 查看crontab帮助: [root@db01 ~]# crontab - ...

  6. 【学习总结】Python-3-风格各异的数值类型实例

    菜鸟教程-Python3-基本数据类型 可能是考点的各种形态的数值类型 int型:正数负数,八进制0开头,十六进制0x开头 float型:小数点的前后都可以没有数字,自动补全 complex型:虚部的 ...

  7. React-Native初识-安卓篇(一)

    前言:React-Native简称RN,可以用来构建Android和IOS的应用程序,在接下来的两个半月里,我会记录下本人在学习RN开发项目中的点滴. 本篇目录: 1.React-Native初识 2 ...

  8. emqtt 分布集群及节点桥接搭建

    目录 分布集群 emq@s1.emqtt.io 节点设置 emq@s2.emqtt.io 节点设置 节点加入集群 节点退出集群 节点发现与自动集群 manual 手动创建集群 基于 static 节点 ...

  9. 第十二章 存储之 Secret

    1.Secret 存在意义 Secret 解决了密码.token.密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec中.Secret 可以以 Volume 或者环境变量的 ...

  10. SQLRecoverableException: I/O Exception: Connection reset

    https://stackoverflow.com/questions/6110395/sqlrecoverableexception-i-o-exception-connection-reset T ...