有几条已建立的连接

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

按 ss 模型,两条 ESTAB、一条 LISTEN,conn_count(rows, "ESTAB") 交回几?

ss 模型:rows = [(状态, 端口)]。listening 交回 LISTEN 的端口;is_listening(rows, 端口) 在不在听;conn_count(rows, 状态) 某状态几条连接。

def listening(rows):
    """rows: [(状态, 端口)];交回处于 LISTEN 的端口列表(按原序)。"""
    return [port for state, port in rows if state == "LISTEN"]


def is_listening(rows, port):
    """某端口在不在监听。"""
    return port in listening(rows)


def conn_count(rows, state):
    """处于某状态(如 ESTAB)的连接有几条。"""
    return sum(1 for s, p in rows if s == state)

print(conn_count([("ESTAB", 80), ("ESTAB", 80), ("LISTEN", 80)], "ESTAB"))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论