← 返回题库
中级

实现API网关路由分发

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    routing_rules_csv = open_url(____).read()
    service_registry_csv = open_url(____).read()
    import pandas as pd
    from io import StringIO
    rules = pd.read_csv(StringIO(____))
    registry = pd.read_csv(StringIO(____))
    def route_request(____):
        for _, rule in rules.iterrows():
            if path.startswith(____):
                if rule['____'] and not is_authenticated:
                    return None, '____'
                healthy = registry[(____) & (____)]
                if healthy.empty: return None, '____'
                inst = healthy.sample(____).iloc[____]
                return {'____':rule['____'],'____':inst['____']}, '____'
        return None, '____'
    paths = [(____),(____),(____),(____),(____)]
    for path, auth in paths:
        inst, msg = route_request(____)
        if inst: print(____)
        else: print(____)

示例

输入
solve()
期望输出
/api/v1/products -> product-service (10.0.2.1:8002)
/api/v1/orders -> order-service (10.0.3.1:8003)
/api/v1/users -> 401 Unauthorized
/api/v2/users -> user-service (10.0.1.2:8001)
/api/v1/unknown -> 404 Not Found
Python 代码 🔒 登录后使用
🔒

登录后即可练习

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