python requests函数封装方法

python  requests函数封装方法

上代码

 1 import requests
 2 import json
 3
 4 """
 5 封装request请求,
 6 1.post:my_post
 7 2.get:my_get
 8 3.返回code:get_code(res)
 9 4.返回json:get_json(res)
10 5.返回text:get_text(res)
11 6.响应时间:get_time(res)
12 7.请求header:get_header(act)
13 9.添加请求头参数:add_header(dict)
14
15 """
16
17 #---------------------------------------------------
18 """
19 r.status_code
20 r.text  #页面内容
21 r.encoding #header中的编码方式
22 r.apparent_encoding  #备选的编码方式
23 r.content  #响应内容的二进制形式
24 timeout=
25 r.elapsed.total_seconds(),单位是s
26 """
27 #----------------------------------------------------
28
29 def  my_post(url,payload,headers,timeout=30):
30     res = requests.request("POST", url, data=payload, headers=headers,timeout=timeout)
31     return res
32
33 def  my_get(url,payload,headers,querystring,timeout=30):
34     resp = requests.request("GET", url, data=payload, headers=headers, params=querystring,timeout=timeout)
35     #获取返回code
36     code=res.status_code
37     print(‘code‘,code)
38     return res
39
40 def get_code(res):
41     #获取返回code
42     code=res.status_code
43     print(‘code:\n‘,code)
44
45 def get_json(res):
46     #获取返回json
47     print(‘res.json:\n‘,res.json())
48     return res.json()
49
50 def get_text(res):
51     print(‘res.text:\n‘,res.text)
52     return res.text
53
54 def get_time(res):
55     #获取响应执行时间,单位s
56     time=res.elapsed.total_seconds()
57     print(‘res.time:\n‘,res.elapsed.total_seconds())
58     return time
59
60 def get_header(act):
61     if act=="json":
62
63         json_header={
64         ‘content-type‘: "application/json",
65         }
66         return json_header
67     else:
68         str_header={
69         ‘content-type‘: "application/x-www-form-urlencoded",
70         }
71         return str_header
72
73 def add_header(dict):
74     headers=get_header("json")
75     for k,v in dict.items():
76         headers[k]=v
77     return headers
78
79
80 if __name__=="__main__":
81
82     url="http://192.168.0.10:3080/asg/portal/call/231.do"
83
84     #json转换格式
85     strData={"pub":{"deviceId":"dz630761d39e7145a3850eedc4563e61ff","subPline":"2","screen":"1080x1920","appCode":"f002","dzPaySupport":"2","userId":"16","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":"4","afu":"0","imei":"864141037331797","p":"55","clientAgent":"svnVer_1907171924","lsw":"1","apn":"wifi","imsi":"460016593620169","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":"10","idStrs":"11000007217:25186137","sign_data":1,"is_sdk":"2","last_rcmbook_id":"","installedFreeApk":0,"index":3,"f":"f0,f1,f2,f3,f4,f5,f6,f7","sex":2,"vtv":"9"}}
86     strJson=json.dumps(strData)
87     print(‘strJson----- ‘,strJson)
88
89     timeout=30
90     headers=get_header("json")
91
92     res=my_post(url,strJson,headers,timeout)
93
94     get_code(res)
95     get_json(res)
96     get_text(res)

原文地址:https://www.cnblogs.com/lisa2016/p/11764068.html

时间: 2024-07-28 20:44:11

python requests函数封装方法的相关文章

python列表函数和方法

Python列表函数和方法 python列表中主要有以下函数: cmp(list1,list2)    比较两个列表的元素 len(list)           列表元素的个数 max(list)           返回列表元素的最大值 min(list)           返回列表元素的最小值 list(seq)           将元组转换成列表 ---------------------------------我是华丽的分割线----------------------------

python基础函数、方法

python的函数和方法,通过def 定义: 函数的特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数和方法的区别:函数有返回值.方法没有 语法定义: def sayhi():#函数名 print("Hello, I'm nobody!") sayhi() #调用函数 可以带参数 #下面这段代码 a,b = 5,8 c = a**b print(c) #改成用函数写 def calc(x,y): res = x**y return res #返回函数执行结果 c = cal

常见的公共函数封装方法(密码强度、手机号验证、邮箱验证、输入金额验证)

//密码复杂度公共函数封装(邮箱,手机号) this.PasswordStrength = function(password) { var rule = Auto517.config.passwordRule.rule; var min = Auto517.config.passwordRule.min; var max = Auto517.config.passwordRule.max; if(rule == 0 && eval('/^[0-9]{' + min + ',' + max

Python中函数和方法的区别

1.函数要手动传self,方法不用传self 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 举例说明: class Foo(object): def __init__(self): self.name="haiyan" def func(self): print(self.name) obj = Foo() obj.func() Foo.func(obj) 判断函数和方法的方式 from types import FunctionType,MethodType obj

python中函数和方法区别,以及如何给python类动态绑定方法和属性(涉及types.MethodType()和__slots__)

网上有很多同义但不同方式的说法,下面的这个说法比较让你容易理解和接受 1 与类和实例无绑定关系的function都属于函数(function): 2 与类和实例有绑定关系的function都属于方法(method). “与类和实例无绑定关系”就道出了其中的关键 我们知道python是动态的编程语言,python的类除了可以预先定义好外,还可以在执行过程中,动态地将函数绑定到类上,绑定成功后,那些函数就变成类的方法了. 定义User类 可以使用__slots__来限制绑定的属性和方法 1 user

Python常用函数、方法总结(持续更新…)

函数 filter() 函数 用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中. filter(function, iterable) def is_odd(n): return n % 2 == 1 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

python基础-函数_递归_内置函数

一 数学定义的函数与python中的函数 二 为何使用函数 背景提要 三 函数和过程 四 函数参数 五 局部变量与全局变量 六 前向引用之'函数即变量' 七 嵌套函数 八 递归 九 匿名函数 十 函数式编程 十一 内置函数 十二 本节作业 一.数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值范围叫做这个函数的定义域

Python基础函数、递归、内置函数

一.数学定义中的函数与Python中的函数 初中数学定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定值,y都有唯一确定的值与之对应,那么我们就把x称为自变量,把y称为因变量,与是x的函数.自变量x的取值范围叫做这个函数的定义域. 例如:y=2*x python中函数的定义:函数是逻辑结构化和过程化的一种编程方法. # Python中函数定义方法 def test(x): "the function definitions" x+=1 return x def

python 06函数

1.格式: python中函数定义方法: def test(x): "The function definitions" x+=1 return x def:定义函数的关键字test:函数名():内可定义形参"":文档描述(非必要,但是强烈建议为你的函数添加描述信息)x+=1:泛指代码块或程序处理逻辑return:定义返回值 调用运行:可以带参数也可以不带函数名() 补充:数学函数和编程函数的不同: 不同的是数学意义的函数,传入值相同,得到的结果必然相同且没有任何变