← 返回题库
初级

按设备类型查询限流配置

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd, json
    cfg_csv = open_url("https://data.zuihe.com/dbd/ms-iot/state_03/rate_limit_config.csv").read()
    cfg = pd.read_csv(StringIO(cfg_csv))
    def get_limit(device_type=None, device_id=None):
        if device_id:
            row = cfg[(cfg['target_type']=='device')&(cfg['target_id']==device_id)]
            if not row.empty: return row.iloc[0].to_dict()
        if device_type:
            row = cfg[(cfg['target_type']=='device_type')&(cfg['target_id']==device_type)]
            if not row.empty: return row.iloc[0].to_dict()
        row = cfg[cfg['target_type']=='global']
        if not row.empty: return row.iloc[0].to_dict()
        return None
    for did, dtype in [('DEV001','temperature'),('DEV004','vibration'),('DEV006','gateway'),('DEV003','pressure')]:
        lim = get_limit(dtype, did)
        if lim: print(f"{did}({dtype}): type={lim['limit_type']}, max={lim['max_requests']}/{lim['window_seconds']}s, action={lim['action']}")

示例

输入
solve()
期望输出
DEV001(temperature): type=sliding_window, max=5/1s, action=reject
DEV004(vibration): type=token_bucket, max=30/60s, action=throttle
DEV006(gateway): type=leaky, max=100/60s, action=alert
DEV003(pressure): type=fixed_window, max=500/60s, action=reject
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码