补全:这条连接归谁

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

本节的 ss 解析小工具(SS_SAMPLE 是真机上 ss -tanp 的原样输出,十行):

parse_ss(text)    一行一个字典:state / recvq / sendq / local / lport / peer / pport / pid(没有进程就是 None)
by_state(rows)    {状态: 行数}|listeners(rows)  监听端口列表|peers_of(rows, port)  端口 port 上(Local 是它)的非 LISTEN 连接 [(状态, 对端端口)]
State      Recv-Q Send-Q Local Address:Port  Peer Address:Port Process
LISTEN     1      5          127.0.0.1:25001      0.0.0.0:*     users:(("python3",pid=46,fd=3))
LISTEN     0      5          127.0.0.1:25000      0.0.0.0:*     users:(("python3",pid=27,fd=3))
TIME-WAIT  0      0          127.0.0.1:31010    127.0.0.1:25002
FIN-WAIT-2 5000   0          127.0.0.1:31001    127.0.0.1:25000 users:(("python3",pid=43,fd=3))
CLOSE-WAIT 1      0          127.0.0.1:25000    127.0.0.1:31001 users:(("python3",pid=27,fd=5))
ESTAB      5      0          127.0.0.1:25001    127.0.0.1:31003
TIME-WAIT  0      0          127.0.0.1:25002    127.0.0.1:31011
ESTAB      0      0          127.0.0.1:31003    127.0.0.1:25001 users:(("python3",pid=49,fd=3))
ESTAB      3976   0          127.0.0.1:31000    127.0.0.1:25000 users:(("python3",pid=30,fd=3))
ESTAB      0      0          127.0.0.1:25000    127.0.0.1:31000 users:(("python3",pid=27,fd=4))

补全 pid_on(rows, lport, pport):找 Local 端口是 lport、对端端口是 pport 的那一行,交回它的 pid(没有进程就交回 None;找不到行交回 -1)。25000↔31001 那条、25001↔31003 那条:

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论