想写一个服务端以便更优雅地访问学校的教务系统,准备用于以后抢课用。第一次用python的fastapi框架,遇到点问题,求助一下各位大哥
async def search_course_by_name(body: SCbyNameBody, headers: dict) -> Tuple[int, dict, dict]:
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{BASE_URL}/PublicQuery/getSelectCourseTermList",
headers=headers,
data={
"termId": body.termId,
"course": body.courseName,
params=param,
if resp.status_code == 200:
courses = [
"courseName": course["kcmc"],
"courseCode": course["kcbh"],
"credit": course["xf"],
"orgnName": course["orgname"]
for course in resp.json()["aaData"]
new_resp = {
"success": True,
"courses": courses
return resp.status_code, resp.headers, new_resp
except Exception as e:
print(e)
return (500, {}, {"success": False,"error": "Internal Server Error"})
else:
return (resp.status_code, resp.headers, {"success": False, "error": "Internal Server Error"})
except (httpx.ReadTimeout, httpx.ConnectTimeout):
return 500, {}, {"error": "Gateway Timeout"}
@app.post("/query/course/byName", include_in_schema=False)
async def byName(body: SCbyNameBody, request: Request):
if body is None:
raise HTTPException(status_code=400, detail="Bad Request")
headers = {
"Cookie": get_cookie(request),
status_code, resp_headers, resp = await search_course_by_name(body, headers)
return JSONResponse(resp, status_code=status_code, headers=resp_headers)
服务端没问题,但是请求时老是报错:
raise ContentDecodingError(e)
requests.exceptions.ContentDecodingError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check'))
打印了以下resp_header
Headers({'server': '', 'date': 'Fri, 08 Mar 2024 07:31:45 GMT',
'content-type': 'application/json;charset=UTF-8',
'transfer-encoding': 'chunked',
'connection': 'keep-alive',
'cache-control': 'no-cache, no-store, max-age=0',
'content-language': 'en-US',
'expires': 'Thu, 01 Jan 1970 00:00:00 GMT',
'pragma': 'no-cache',
'x-frame-options': 'SAMEORIGIN',
'x-ua-compatible': 'IE=edge,chrome=1',
'content-encoding': 'gzip'})