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. Windows下使用Composer安装yii2

    Composer简介 Composer 是PHP中用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer会帮你安装这些依赖的 ...

  2. Day 45 Mysql 数据库练习题二

    1.表关系   注意:创建表时,根据合理性设置字段的长度和类型. 2.下面:开始你的表演 1.查询所有人员信息 select * from ren 2.只查询人员的姓名和年龄 select name, ...

  3. FWT公式一览

    总表 真值表 对应运算 FWT IFWT A=B=0 A≠B A=B=1 左项 右项 左项 右项 0 0 1 & L+R R L-R R 0 1 0 ^ L+R L-R (L+R)/2 (L- ...

  4. 创建调用Consul的客户端项目

    创建调用Consul的客户端项目 创建项目 源码 Github仓库:https://github.com/sunweisheng/spring-cloud-example

  5. opencv中图像的读取,显示与保存1

    1.读入图像 用cv2.imread()函数来读取图像,cv2.imread(路径,图像颜色空间)(其中颜色空间默认为BGR彩图)     cv2.IMREAD_COLOR:读入一副彩色图像 cv2. ...

  6. 向服务器发送post请求

    /** * 通过HttpClient发送Post请求 * @param path 请求路径 * @param params 请求参数 * @param encoding 编码 * @return 请求 ...

  7. 配置阿里云SLB全站HTTPS集群

    配置阿里云SLB全站HTTPS集群(以下内容仅为流程,信息可能有些对应不上) 1 登录阿里云购买两台实例 1.1 按量付费购买两台实例 1.2 配置网络可以不选择分配外网 1.3 自定义密码 1.4 ...

  8. Python 第三天学习整理2

    一.常用的字符串方法 capitalize() #将字符串首字母大写 center(100,'*') #把字符串居中的 count(‘zhou’)#查询次数 endswith('.jpg ')#判断字 ...

  9. 回收子进程——wait/waitpid 与 信号机制

    孤儿/僵尸进程——回收子进程 参考博客:https://blog.csdn.net/qq_35396127/article/details/78725915 :https://www.cnblogs. ...

  10. SQL的子查询与JOIN的小试牛刀

    //学生表CREATE TABLE student( ID INT PRIMARY KEY, s_name ) NOT NULL, class_id INT NOT NULL); , "qf ...