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. C++的指针偏移

    假设一个类的定义如下:class Ob{public:Ob() : a(1), b(10) {}int a;private:int b; };

  2. 测开之路三十一:Flask基础之请求与相应

    from flask import requestrequest.pathrequest.methodrequest.formrequest.argsrequest.values 一般用form获取p ...

  3. Mac-如何安装apk到android手机

    将电脑上的apk安装到手机,Windows系统可以使用usb连接Android手机,然后打开编辑手机中的文件,直接粘贴apk到手机上安装apk.对于Mac来说就没有那么简单啦.那么Mac如何将apk安 ...

  4. exporter

    何为 Prometheus Exporter? Prometheus 监控基于一个很简单的模型: 主动抓取目标的指标接口(HTTP 协议)获取监控指标, 再存储到本地或远端的时序数据库. Promet ...

  5. Python运行出错

    (1)ValueError: You are trying to load a weight file containing 6 layers into a model with 5 layers. ...

  6. Centos7 安装rar,unrar,zip,unzip

    1.yum 安装unrar # yum install unrar rar命令 # rar a test.rar test.jpg test.png 这条命令是将test.jpg和test.png压缩 ...

  7. 前端工程师的新选择WebApp

    作为新一代移动端应用分发入口,小程序的趋势明朗化,竞争也在急剧激烈化.战线从手机 QQ.QQ 浏览器.支付宝.手机淘宝,华为,小米等九家手机厂商推出“快应用”,再拉到了谷歌的 Instant App ...

  8. 2019-10-31-WPF-等距布局

    title author date CreateTime categories WPF 等距布局 lindexi 2019-10-31 9:0:2 +0800 2018-2-21 17:3:4 +08 ...

  9. 2018-2-13-win10-UWP-应用设置

    title author date CreateTime categories win10 UWP 应用设置 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  10. python安装pika模块rabbitmq

    1.pip install pika 2.如找不到 拷贝 D:\python\testmq\venv\Lib\site-packages  \pika目录