python封装https请求

 1 import http.client
 2 import json
 3 
 4 class HTTPS_Connection:
 5 
 6     def __init__(self, res_type, body, url, api_path, headers):
 7         self.res_type = res_type  # 接口请求类型
 8         self.body = body          # 请求参数
 9         self.url = url            # 请求服务地址
10         self.path = api_path      # 接口路由
11         self.headers = headers    # 请求头
12 
13     def https_res(self):
14         conn = http.client.HTTPSConnection(self.url)
15         body = json.dumps(self.body)
16         conn.request(method=self.res_type, url=self.path, body=body, headers=self.headers)
17         res = conn.getresponse()
18         response = res.read()
19         print('接口返回结果:%s' % response.decode('utf-8'))
20         return response.decode('utf-8')
21 
22 
23 if __name__ == '__main__':
24     pass

 

posted @ 2023-10-16 14:00  半路出家_24  阅读(109)  评论(0)    收藏  举报