Browse Source

fix:修复登陆的报错问题与requests库校验

TzuxinChen 3 months ago
parent
commit
4f2b4ffdfd
6 changed files with 20 additions and 12 deletions
  1. 2 2
      .env.example
  2. 2 2
      common/setting.py
  3. 3 3
      common/utils.py
  4. 1 1
      main.py
  5. 4 0
      requirements.txt
  6. 8 4
      util/login.py

+ 2 - 2
.env.example

@@ -1,4 +1,4 @@
 BASE_URL=http://localhost:8000
-USERNAME=admin
-PASSWORD=password
+API_USERNAME=admin
+API_PASSWORD=password
 DB_URL="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable"

+ 2 - 2
common/setting.py

@@ -17,12 +17,12 @@ class Config(BaseSettings):
         default='http://127.0.0.1:8000'
     )
 
-    USERNAME: str = Field(
+    API_USERNAME: str = Field(
         description='username',
         default='admin'
     )
 
-    PASSWORD: str = Field(
+    API_PASSWORD: str = Field(
         description='password',
         default='admin'
     )

+ 3 - 3
common/utils.py

@@ -19,15 +19,15 @@ def call_api(endpoint, method="GET", params=None, data=None) -> dict:
     for i in range(3):
         try:
             if method.upper() == "GET":
-                response = requests.get(url, params=params, cookies=cookies)
+                response = requests.get(url, params=params, cookies=cookies, verify=False)
             elif method.upper() == "POST":
-                response = requests.post(url, json=data, params=params, cookies=cookies)
+                response = requests.post(url, json=data, params=params, cookies=cookies, verify=False)
             else:
                 raise ValueError("Unsupported HTTP method")
             logger.info(f"Calling API: {endpoint}, Method: {method}, Params: {params}, Data: {data}, ResCode:{response.status_code}, ResData: {response.text}")
             if response.status_code == 502:
                 cookies = LoginApi().login_bmc(config.USERNAME, config.PASSWORD)
-                raise NoValidError()
+                continue
             response.raise_for_status()
             return response.json()
         except requests.RequestException as e:

+ 1 - 1
main.py

@@ -18,7 +18,7 @@ class Main:
         #     all_unaccepted_event_list.extend(unaccepted_event_list.get('eventList'))
         # save_json(all_unaccepted_event_list, "result/event/unaccepted_event_list.json")
 
-        for i in range(1, 26):
+        for i in range(1, 2):
             unaccepted_event_list = self.api.query_event_list("accepted_event_view", pageIndex=i)
             all_accepted_event_list.extend(unaccepted_event_list.get('eventList'))
         save_json(all_accepted_event_list, "result/event/accepted_event_list.json")

+ 4 - 0
requirements.txt

@@ -0,0 +1,4 @@
+pydantic==2.10.3
+pydantic_settings==2.6.1
+Requests==2.32.3
+SQLAlchemy==1.4.49

+ 8 - 4
util/login.py

@@ -1,6 +1,11 @@
 import requests
 from typing import Dict, Any
-from common.setting import Config
+
+from sympy.polys.polyconfig import configure
+
+from common import config
+from common.utils import call_api
+
 
 class LoginApi:
     def __init__(self):
@@ -17,8 +22,7 @@ class LoginApi:
         :return: 包含响应数据的字典
         :raises: 请求错误或响应异常
         """
-        url = f"{Config.BASE_URL}/login"
-
+        url = f'{config.BASE_URL}/login'
         # 构造请求参数
         params = {
             "username": username,
@@ -28,7 +32,7 @@ class LoginApi:
 
         try:
             # 发送GET请求
-            response = requests.get(url, params=params)
+            response = requests.get(url, params=params, verify=False)
 
             # 处理响应
             if response.status_code == 200: