1.python 做的request模块封装工具类
记录下,避免以后用到。
import time
from requests.adapters import HTTPAdapter
import json
import requests
from requests import exceptions
class HttpRequests(object):
"""
eg: request = HttpRequests()
response = request(method, url, data)
or
response = request.send_request(method, url, data)
print(response.text)
"""
def __init__(self):
self.session = requests.Session()
self.session.mount('http://', HTTPAdapter(max_retries=3))
self.session.mount('https://', HTTPAdapter(max_retries=3))
# exceptions.Timeout
def send_request(self, method, url, data=None, params_type='JSON', **kwargs):
"""
request 请求封装 默认使用json格式
"""
method = method.upper()
params_type = params_type