目前的框架仅支持HTTP协议的请求,使用requests库发送请求。工具类封装了两种发送请求的模式:一种是无状态的请求;另一种是记录session信息的请求。
以下是V1版本,后续优化。
# -*- coding:UTF-8 -*-
# @author : Joker
# @Time : 2019/12/25
# @IDE : PyCharm
# @Version : Python 3.7
"""发送HTTP请求的模块"""
import requests
class CommonRequest:
"""发送一般的请求(无状态)"""
def send(self, url, method, params=None, data=None, json=None, headers=None):
method = method.upper()
if method == "GET":
return requests.get(url=url, params=params, headers=headers)
elif method == "POST":
return requests.post(url=url, data=data, json=json, headers=headers)
elif method == "PATCH":
return requests.patch(url=url, data=data, json=json, headers=headers)
class SessionRequest:
"""发送记录Session信息的请求"""
def __init__(self):
self.se = requests.session()
def send(self, url, method, params=None, data=None, json=None, headers=None):
method = method.upper()
if method == "GET":
return self.se.get(url=url, params=params, headers=headers)
elif method == "POST":
return self.se.post(url=url, data=data, json=json, headers=headers)
elif method == "PATCH":
return self.se.patch(url=url, data=data, json=json, headers=headers)