File size: 1,955 Bytes
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import os


# 注册/更新进数据库
def app_register_user(data, url="http://localhost:8000/users/regByCamera"):
    # 发起POST请求
    api_key = os.environ["Mama_API_Key"]  # 获取token
    response = requests.post(url,
                             json=data,
                             headers={"Content-Type": "application/json",
                                      "mama_api_key": api_key})

    # 获取JSON响应
    if response.status_code == 200:
        # 检查响应是否为空
        if response.text.strip():
            try:
                json_response = response.json()
                print(json_response)
            except ValueError as e:
                print(f"解析JSON响应失败: {e}")
                return []
        else:
            print("回应为空")
            return []
    else:
        print(f"回应失败状态码:{response.status_code}")
        return []


# 从数据库获取被动用户数据
def app_get_user(url="http://localhost:8000/users/getUserInfo"):
    # 发起GET请求
    api_key = os.environ["Mama_API_Key"]  # 获取token
    response = requests.get(url,
                            headers={"Content-Type": "application/json",
                                     "mama_api_key": api_key})
    # 获取响应内容
    if response.status_code == 200:
        # 检查响应是否为空
        if response.text.strip():
            try:
                data_list = response.json()
                return data_list
            except ValueError as e:
                print(f"解析JSON响应失败: {e}")
                return []
        else:
            print("回应为空")
            return []
    else:
        print(f"回应失败状态码:{response.status_code}")
        return []


if __name__ == "__main__":
    # app_register_user() # 注册/更新数据库
    # app_get_user() # 从数据库获取被动用户数据
    print(app_get_user())