← 返回题库
初级

设计设备操作HTTP状态码规范

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd
    devs_csv = open_url("https://data.zuihe.com/dbd/ms-iot/state_00/devices.csv").read()
    devs = pd.read_csv(StringIO(devs_csv))
    def upload_data(device_id, payload):
        row = devs[devs['device_id']==device_id]
        if row.empty: return 404, {'error': 'device not found'}
        st = row.iloc[0]['status']
        if st == 'maintenance': return 503, {'error': 'device in maintenance'}
        if st == 'error': return 422, {'error': 'device error state'}
        if not payload.get('readings'): return 400, {'error': 'missing readings'}
        return 200, {'accepted': True, 'device_id': device_id}
    tests = [('DEV001',{'readings':[1]}),('DEV099',{}),('DEV010',{'readings':[1]}),('DEV001',{})]
    for did, payload in tests:
        code, body = upload_data(did, payload)
        print(f"{did}: HTTP {code} - {body}")

示例

输入
solve()
期望输出
DEV001: HTTP 200 - {'accepted': True, 'device_id': 'DEV001'}
DEV099: HTTP 404 - {'error': 'device not found'}
DEV010: HTTP 503 - {'error': 'device in maintenance'}
DEV001: HTTP 400 - {'error': 'missing readings'}
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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