python-对requests请求简单的封装

时间:2022-10-08 06:29:27
 # coding:utf-8

 import requests

 class send_request:

     def __init__(self,url,method,data=None):
self.response = self.run_main(url,method,data) def send_get(self, url, data=None):
headers = {'content-type': 'charset=utf8'}
response = requests.get(url=url, data=data, headers=headers)
return response.content.decode('utf-8') def send_post(self, url, data):
headers = {'content-type': 'charset=utf8'}
response = requests.post(url=url, data=data,headers=headers)
return response.content.decode('utf-8') def run_main(self, url, method, data=None):
response = None
if method == 'get':
response = self.send_get(url, data)
elif method == 'post':
response = self.send_post(url, data)
else:
response = 'the method is error'
return response if __name__ == '__main__':
url = 'https://www.baidu.com'
data = { }
response = send_request(url=url, method='get')
print(response.response)