修改requests库为httpx
This commit is contained in:
parent
4e72940da6
commit
9cf9596299
|
@ -0,0 +1,18 @@
|
|||
2023-07-11 15:02:10.354 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:02:24.175 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:02:24.300 | WARNING | utils.requests_process.requests_control:cookie_token:34 - 未获取到登录认证信息,请检查: [Errno 8] nodename nor servname provided, or not known
|
||||
2023-07-11 15:03:30.032 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:04:07.974 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:04:46.583 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:05:12.269 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:06:10.763 | WARNING | conftest:<module>:100 - 当前配置库未配置或配置有误
|
||||
2023-07-11 15:06:10.888 | WARNING | utils.requests_process.requests_control:cookie_token:32 - 未获取到登录认证信息,请检查: [Errno 8] nodename nor servname provided, or not known
|
||||
2023-07-11 15:06:11.166 | INFO | utils.requests_process.requests_control:wrapper:66 -
|
||||
请求地址: https://open.xwdsp.com/phegda/advrest
|
||||
请求方法: POST
|
||||
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
|
||||
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '2', 'styleId': [1004]}], 'requestId': '16805156922345810'}
|
||||
响应数据: {'data': {'ad': [], 'bidId': 'e2666ab3db636ad6356311ead83061ca', 'requestId': '16805156922345810'}, 'success': True}
|
||||
响应耗时(ms): 275.0
|
||||
接口响应码: 200
|
||||
|
|
@ -9,7 +9,7 @@ pytest==7.2.0
|
|||
pytest-ordering==0.6
|
||||
pytest-rerunfailures==11.1
|
||||
PyYAML==6.0
|
||||
requests==2.28.1
|
||||
httpx==*
|
||||
click==8.1.3
|
||||
urllib3==1.26.12
|
||||
python-jenkins==1.7.0
|
||||
|
|
Binary file not shown.
|
@ -2,18 +2,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2023/4/27 09:51
|
||||
# @Author : 谈林海
|
||||
from functools import lru_cache
|
||||
from functools import lru_cache, wraps
|
||||
|
||||
|
||||
def singleton(cls):
|
||||
"""单例模式装饰器"""
|
||||
|
||||
instances = {}
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def get_instance(*args, **kwargs):
|
||||
key = (cls, args, tuple(sorted(kwargs.items())))
|
||||
@wraps(cls)
|
||||
def wrapper(*args, **kwargs):
|
||||
key = (cls, args, frozenset(kwargs.items()))
|
||||
if key not in instances:
|
||||
instances[key] = cls(*args, **kwargs)
|
||||
return instances[key]
|
||||
|
||||
return get_instance
|
||||
return wrapper
|
Binary file not shown.
|
@ -3,34 +3,28 @@
|
|||
# @Time : 2023/4/24 15:59
|
||||
# @Author : 谈林海
|
||||
import json
|
||||
import urllib3
|
||||
from urllib3.util.retry import Retry
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
from requests.exceptions import RetryError, Timeout, RequestException
|
||||
from functools import wraps
|
||||
|
||||
import httpx
|
||||
from utils import *
|
||||
from utils.data_process.json_control import JsonHandler
|
||||
from utils.requests_process.create_cookie_control import Cookies
|
||||
from utils.commons.allure_control import ReportStyle
|
||||
from utils.commons.singleton_control import singleton
|
||||
from utils.data_process.json_control import JsonHandler
|
||||
from utils.requests_process.create_cookie_control import Cookies
|
||||
|
||||
payload = {"account": config.account,
|
||||
"password": config.password
|
||||
}
|
||||
|
||||
|
||||
@singleton
|
||||
class Authentication:
|
||||
"""获取token/cookies"""
|
||||
|
||||
def __init__(self):
|
||||
# 读取全局配置中的账号信息
|
||||
self.payload = {"account": config.account,
|
||||
"password": config.password
|
||||
}
|
||||
|
||||
@property
|
||||
def cookie_token(self):
|
||||
@staticmethod
|
||||
def cookie_token():
|
||||
try:
|
||||
res = requests.post('https://tianqiai/login', data=self.payload)
|
||||
res = httpx.post('https://tianqiai/login', data=payload)
|
||||
js = JsonHandler(res.json())
|
||||
res_cookies, res_token = res.cookies, js.find_one('$..token')
|
||||
return res_cookies, res_token
|
||||
|
@ -41,35 +35,13 @@ class Authentication:
|
|||
|
||||
@singleton
|
||||
class RestClient:
|
||||
"""封装api请求类"""
|
||||
"""
|
||||
基于 httpx 封装的同步请求类
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
timeout=10,
|
||||
retry=3,
|
||||
backoff_factor=0.3,
|
||||
proxies=None
|
||||
):
|
||||
# 初始化函数,设置请求超时时间、重试次数、backoff因子(指定失败后下一次重试时间间隔的增长因子)、代理等参数
|
||||
# 这四个参数都是默认设置的,用户可以根据需要进行更改
|
||||
urllib3.disable_warnings()
|
||||
self.timeout = timeout # 超时时间
|
||||
self.proxies = proxies or {} # 设置代理
|
||||
self.session = requests.session() # 创建会话对象
|
||||
self.session.Timeout = Timeout(timeout) # 设置超时时间
|
||||
self.session.verify = False
|
||||
self.session.cookies, self.token = Authentication().cookie_token
|
||||
self.session.proxies.update(self.proxies)
|
||||
|
||||
retry_strategy = Retry(
|
||||
total=retry,
|
||||
backoff_factor=backoff_factor,
|
||||
status_forcelist=[429, 500, 502, 503, 504],
|
||||
)
|
||||
|
||||
adapter = HTTPAdapter(max_retries=retry_strategy)
|
||||
self.session.mount("https://", adapter)
|
||||
self.session.mount("http://", adapter)
|
||||
# 设置重试策略并将其与会话对象关联,以便每次请求失败时自动重试
|
||||
def __init__(self):
|
||||
self.cookies, self.token = Authentication.cookie_token()
|
||||
self.client = httpx.Client(cookies=self.cookies, timeout=30, verify=False)
|
||||
|
||||
def res_log(func):
|
||||
@wraps(func)
|
||||
|
@ -100,6 +72,7 @@ class RestClient:
|
|||
ReportStyle.allure_step_no(f"响应耗时(ms): {round(res.elapsed.total_seconds() * 1000)}")
|
||||
ReportStyle.allure_step("响应数据", resp)
|
||||
return res.status_code if isinstance(resp, int) else res
|
||||
|
||||
return wrapper
|
||||
|
||||
@res_log
|
||||
|
@ -108,10 +81,12 @@ class RestClient:
|
|||
url, request_method, _ = args[0]
|
||||
except ValueError:
|
||||
url, request_method = args[0]
|
||||
res = getattr(self.session, request_method.lower())(url, **kwargs)
|
||||
# 发起请求
|
||||
res = self.client.request(request_method, url, **kwargs)
|
||||
# 接口请求结果
|
||||
try:
|
||||
return res
|
||||
except (RequestException, Timeout, RetryError) as err:
|
||||
except Exception as err:
|
||||
logger.error(f'请求失败: {err}')
|
||||
return None
|
||||
|
||||
|
@ -125,4 +100,3 @@ class RestClient:
|
|||
return headers
|
||||
|
||||
res_log = staticmethod(res_log)
|
||||
|
||||
|
|
Loading…
Reference in New Issue