第一天任务,封装请求方法
http_request.py
# -*- encoding: utf-8 -*-
"""
@File : http_request.py
@Time : 2022/11/7 15:41
@Author : swithin
@Software: PyCharm
"""
import requests
class HttpRequest:
"""
:param url:
:param param:
:param method:
:return:
"""
def http_request(self, url, method=None, params=None, data=None, json=None, headers=None):
if method.lower() == "get":
try:
res = requests.get(url=url, params=params, headers=headers)
except Exception as e:
print("get request fail,error is :{0}".format(e))
elif method.lower() == "post":
try:
res = requests.post(url=url, data=data, json=json, headers=headers, )
except Exception as e:
print("post request fail,error is :{0}".format(e))
elif method.lower() == "put":
try:
res = requests.put(url=url, data=data, json=json, headers=headers)
except Exception as e:
print("put request fail,error is :{0}".format(e))
elif method.lower() == "delete":
try:
res = requests.delete(url=url, data=data, json=json, headers=headers)
except Exception as e:
print("delete request fail,error is :{0}".format(e))
else:
print("method undefined")
return res
if __name__ == "__main__":
res = HttpRequest()
url = "http://127.0.0.1/login"
param = {
"username": "admin",
"password": "123456",
"uuid": "360bf1a4cf9e46f387521fca331a6c4f"
}
method = "post"
res = res.http_request(url, method=method, json=param)
print(res.text)