MihoyoBBSTools/request.py

26 lines
758 B
Python
Raw Normal View History

2021-05-23 13:24:20 +00:00
import httpx
#这里实际上应该加个"-> dict"但是考虑到请求可能失败的关系,所以直接不声明返回变量
def get(url:str, **headers:dict):
try:
req = httpx.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
def post(url:str, data:dict, **headers:dict):
try:
req = httpx.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
def post_json(url:str, json, **headers:dict):
try:
req = httpx.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-06-06 13:19:28 +00:00
return ""