2021-06-13 02:33:38 +00:00
|
|
|
|
try:
|
2021-10-25 14:53:34 +00:00
|
|
|
|
# 优先使用httpx,在httpx无法使用的环境下使用requests
|
2021-06-13 02:33:38 +00:00
|
|
|
|
import httpx
|
2021-10-25 14:53:34 +00:00
|
|
|
|
|
2021-12-02 02:54:24 +00:00
|
|
|
|
http = httpx.Client(timeout=10, transport=httpx.HTTPTransport(retries=5))
|
2021-10-25 14:53:34 +00:00
|
|
|
|
# 当openssl版本小于1.0.2的时候直接进行一个空请求让httpx报错
|
2021-06-14 00:05:37 +00:00
|
|
|
|
import tools
|
2022-01-06 05:49:25 +00:00
|
|
|
|
if tools.get_openssl_version() <= 102:
|
2021-06-14 00:05:37 +00:00
|
|
|
|
httpx.get()
|
|
|
|
|
except:
|
2021-06-13 02:33:38 +00:00
|
|
|
|
import requests
|
2021-12-02 02:54:24 +00:00
|
|
|
|
from requests.adapters import HTTPAdapter
|
|
|
|
|
http = requests.Session()
|
|
|
|
|
http.mount('http://', HTTPAdapter(max_retries=5))
|
|
|
|
|
http.mount('https://', HTTPAdapter(max_retries=5))
|
2021-05-23 13:24:20 +00:00
|
|
|
|
|
2021-10-25 14:53:34 +00:00
|
|
|
|
|
|
|
|
|
# 这里实际上应该加个"-> dict"但是考虑到请求可能失败的关系,所以直接不声明返回变量
|
|
|
|
|
def get(url: str, **headers: dict):
|
2021-05-23 13:24:20 +00:00
|
|
|
|
try:
|
2021-06-13 02:33:38 +00:00
|
|
|
|
req = http.get(url, headers=headers)
|
2021-06-06 13:19:28 +00:00
|
|
|
|
return req.json()
|
2021-05-23 13:24:20 +00:00
|
|
|
|
except:
|
|
|
|
|
print("请求失败,网络错误!")
|
2021-06-06 13:19:28 +00:00
|
|
|
|
return ""
|
2021-05-23 13:24:20 +00:00
|
|
|
|
|
2021-10-25 14:53:34 +00:00
|
|
|
|
|
|
|
|
|
def post(url: str, data: dict, **headers: dict):
|
2021-05-23 13:24:20 +00:00
|
|
|
|
try:
|
2021-06-13 02:33:38 +00:00
|
|
|
|
req = http.post(url, data=data, headers=headers)
|
2021-06-06 13:19:28 +00:00
|
|
|
|
return req.json()
|
2021-05-23 13:24:20 +00:00
|
|
|
|
except:
|
|
|
|
|
print("请求失败,网络错误!")
|
2021-06-06 13:19:28 +00:00
|
|
|
|
return ""
|
2021-05-23 13:24:20 +00:00
|
|
|
|
|
2021-10-25 14:53:34 +00:00
|
|
|
|
|
|
|
|
|
def post_json(url: str, json, **headers: dict):
|
2021-05-23 13:24:20 +00:00
|
|
|
|
try:
|
2021-06-13 02:33:38 +00:00
|
|
|
|
req = http.post(url, json=json, headers=headers)
|
2021-06-06 13:19:28 +00:00
|
|
|
|
return req.json()
|
2021-05-23 13:24:20 +00:00
|
|
|
|
except:
|
|
|
|
|
print("请求失败,网络错误!")
|
2021-10-25 14:53:34 +00:00
|
|
|
|
return ""
|