python requests函数封装方法
Posted Golover
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: ‘,code) 44 45 def get_json(res): 46 #获取返回json 47 print(‘res.json: ‘,res.json()) 48 return res.json() 49 50 def get_text(res): 51 print(‘res.text: ‘,res.text) 52 return res.text 53 54 def get_time(res): 55 #获取响应执行时间,单位s 56 time=res.elapsed.total_seconds() 57 print(‘res.time: ‘,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)
以上是关于python requests函数封装方法的主要内容,如果未能解决你的问题,请参考以下文章