-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
86 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,44 @@ | ||
import json | ||
import logging | ||
import tools | ||
import request | ||
from typing import Dict | ||
from loghelper import log | ||
|
||
import requests | ||
from requests import HTTPError, Response | ||
|
||
|
||
def http_get(url: str, max_retries: int = 2, **kwargs) -> Response: | ||
def http_get(url: str, max_retries: int = 2, **kwargs): | ||
return http_request("get", url, max_retries, **kwargs) | ||
|
||
|
||
def http_get_json(url: str, max_retries: int = 2, **kwargs) -> Dict[str, any]: | ||
resp = http_get(url, max_retries, **kwargs) | ||
data = resp.text | ||
return json.loads(data) | ||
return resp.json() | ||
|
||
|
||
def http_post(url: str, max_retries: int = 2, **kwargs) -> Response: | ||
def http_post(url: str, max_retries: int = 2, **kwargs): | ||
return http_request("post", url, max_retries, **kwargs) | ||
|
||
|
||
def http_post_json(url: str, max_retries: int = 2, **kwargs) -> Dict[str, any]: | ||
resp = http_post(url, max_retries, **kwargs) | ||
data = resp.text | ||
return json.loads(data) | ||
return resp.json() | ||
|
||
|
||
def http_request( | ||
method: str, | ||
url: str, | ||
max_retries: int = 2, | ||
**kwargs | ||
) -> Response: | ||
method: str, | ||
url: str, | ||
max_retries: int = 2, | ||
**kwargs | ||
): | ||
for i in range(max_retries + 1): | ||
try: | ||
logging.debug(f"{method.upper()} {url}, REQ: {i+1}/{max_retries}") | ||
session = requests.Session() | ||
log.debug(f"{method.upper()} {url}, REQ: {i + 1}/{max_retries}") | ||
session = request.get_new_session() | ||
session.headers["USER_AGENT"] = tools.get_useragent() | ||
resp = session.request(method, url, **kwargs) | ||
logging.debug(f"Response: {resp.status_code}\n\n{resp.text}\n") | ||
except HTTPError as e: | ||
logging.error(f"HTTP error: {e}, REQ: {i+1}/{max_retries}") | ||
log.debug(f"Response: {resp.status_code}\n\n{resp.text}\n") | ||
except KeyError as e: | ||
logging.error(f"Wrong response: {e}, REQ: {i+1}/{max_retries}") | ||
log.error(f"Wrong response: {e}, REQ: {i + 1}/{max_retries}") | ||
except Exception as e: | ||
logging.error(f"Unknown error: {e}, REQ: {i+1}/{max_retries}") | ||
log.error(f"HTTP /Unknown error: {e}, REQ: {i + 1}/{max_retries}") | ||
else: | ||
return resp | ||
raise Exception(f"所有HTTP请求失败,请检查网络") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
try: | ||
# 优先使用httpx,在httpx无法使用的环境下使用requests | ||
import httpx | ||
def get_new_session(): | ||
try: | ||
# 优先使用httpx,在httpx无法使用的环境下使用requests | ||
import httpx | ||
|
||
http = httpx.Client(timeout=20, transport=httpx.HTTPTransport(retries=10)) | ||
# 当openssl版本小于1.0.2的时候直接进行一个空请求让httpx报错 | ||
import tools | ||
http = httpx.Client(timeout=20, transport=httpx.HTTPTransport(retries=10)) | ||
# 当openssl版本小于1.0.2的时候直接进行一个空请求让httpx报错 | ||
import tools | ||
|
||
if tools.get_openssl_version() <= 102: | ||
httpx.get() | ||
except (TypeError, ModuleNotFoundError): | ||
import requests | ||
from requests.adapters import HTTPAdapter | ||
if tools.get_openssl_version() <= 102: | ||
httpx.get() | ||
except (TypeError, ModuleNotFoundError): | ||
import requests | ||
from requests.adapters import HTTPAdapter | ||
|
||
http = requests.Session() | ||
http.mount('http://', HTTPAdapter(max_retries=10)) | ||
http.mount('https://', HTTPAdapter(max_retries=10)) | ||
http = requests.Session() | ||
http.mount('http://', HTTPAdapter(max_retries=10)) | ||
http.mount('https://', HTTPAdapter(max_retries=10)) | ||
return http | ||
|
||
|
||
http = get_new_session() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters