Python 常用标准库盘点

前言 Python “自带电池”——标准库覆盖了运维脚本 80% 的需求。这篇盘点我使用频率最高的那些,每个都给实战片段。 一、os / sys / pathlib:系统交互 import os, sys from pathlib import Path # 环境与退出 os.environ.get("HOME", "/root") # 读环境变量(带默认值) os.getpid(), os.getcwd() sys.exit(1) # 带状态码退出(脚本规范) # pathlib 三板斧(上篇讲过, 这里补充) for f in Path("/var/log").rglob("*.log"): # 递归找 print(f, f.stat().st_size) # 执行外部命令的正确姿势见 subprocess(别用 os.system) 二、collections:数据结构增强 from collections import Counter, defaultdict, deque, OrderedDict # Counter: 计数器之王 c = Counter(["go", "py", "go", "sh", "go"]) c.most_common(2) # [('go', 3), ('py', 1)] c["go"] # 3(不存在的 key 返回 0, 不报错) # defaultdict: 消灭 if key not in groups = defaultdict(list) for name, dept in [("a", "ops"), ("b", "dev"), ("c", "ops")]: groups[dept].append(name) # {'ops': ['a', 'c'], 'dev': ['b']} logs = defaultdict(int) # 默认 0: 直接 += 1 不会 KeyError logs["error"] += 1 # deque: 双端队列(队列/栈/滑动窗口) q = deque(maxlen=5) for i in range(10): q.append(i) # 自动挤掉最老的 # deque([5, 6, 7, 8, 9]) q.appendleft(0) # O(1) 头部插入, list 是 O(n) 三、itertools:迭代器魔法 from itertools import chain, islice, groupby, product, combinations # chain: 拼接多个可迭代对象 list(chain([1, 2], [3], range(4, 6))) # [1, 2, 3, 4, 5] # islice: 切迭代器(不能下标的对象) list(islice(range(100), 5, 10)) # [5, 6, 7, 8, 9] # product: 笛卡尔积(替代多层 for) for env, svc in product(["prod", "staging"], ["web", "api"]): print(f"{env}-{svc}") # groupby: 分组(注意: 先排序再分组!) data = sorted([("a", 1), ("b", 2), ("a", 3)], key=lambda x: x[0]) for k, g in groupby(data, key=lambda x: x[0]): print(k, list(g)) # a [('a',1),('a',3)] / b [('b',2)] 四、datetime:时间处理 from datetime import datetime, timedelta, timezone now = datetime.now() utc = datetime.now(timezone.utc) # 时区 aware(推荐!) # 格式化与解析 now.strftime("%Y-%m-%d %H:%M:%S") # '2024-06-15 10:30:00' datetime.strptime("2024-06-15", "%Y-%m-%d") # 字符串 -> datetime datetime.fromisoformat("2024-06-15T10:00") # ISO 格式直解 # 运算 yesterday = now - timedelta(days=1) (now - yesterday).days # 1 now.timestamp() # -> Unix 时间戳 datetime.fromtimestamp(1718419200) # <- Unix 时间戳 # 时区转换 tz_sh = timezone(timedelta(hours=8)) utc.astimezone(tz_sh) # UTC -> 北京时间 五、subprocess:执行命令(重点) import subprocess # ✅ 推荐写法: run + capture(3.5+) r = subprocess.run( ["df", "-h"], # 列表形式(不经 shell, 更安全) capture_output=True, text=True, timeout=10, ) print(r.returncode) # 0 print(r.stdout) # 带 shell 管道时才用 shell=True(注意注入风险!) r = subprocess.run("df -h | grep data", shell=True, capture_output=True, text=True) # 失败抛异常(写脚本报错即停) r = subprocess.run(["ls", "/nope"], check=True, capture_output=True, text=True) # 实战: 检查服务是否存活 def service_active(name: str) -> bool: r = subprocess.run(["systemctl", "is-active", name], capture_output=True, text=True) return r.stdout.strip() == "active" 六、argparse:像样的命令行参数 import argparse p = argparse.ArgumentParser(description="主机巡检工具") p.add_argument("hosts", nargs="+", help="目标主机列表") p.add_argument("-p", "--port", type=int, default=22) p.add_argument("-v", "--verbose", action="store_true") args = p.parse_args() # python check.py web01 web02 -p 2222 -v # args.hosts=['web01','web02'] args.port=2222 args.verbose=True 七、json / re / hashlib / secrets:日常刚需 import json, re, hashlib, secrets # json(上篇详述) json.dumps({"k": "值"}, ensure_ascii=False, indent=2) # re: 三个够用的函数 re.search(r"(\d+\.\d+\.\d+\.\d+)", text) # 找第一处(返回 match) re.findall(r"ERROR.*", log) # 找全部 re.sub(r"\s+", " ", s) # 替换 # 预编译(循环里高频使用时) IP = re.compile(r"^(?:\d{1,3}\.){3}\d{1,3}$") IP.match("10.0.0.1") # hashlib: 校验文件 h = hashlib.sha256() for chunk in iter(lambda: open("app.tar.gz","rb").read(8192), b""): h.update(chunk) print(h.hexdigest()) # secrets: 生成安全随机串(密码/Token, 别用 random!) secrets.token_urlsafe(32) 八、logging:别再 print 了 import logging logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) log = logging.getLogger(__name__) log.info("开始巡检 %s", "web01") # 惰性格式化, 占位符写法 try: 1 / 0 except ZeroDivisionError: log.exception("计算失败") # 自动带堆栈 九、综合实战:一行式巡检脚本 #!/usr/bin/env python3 """快速体检: 磁盘/内存/负载""" import subprocess, shutil, os def sh(cmd: str) -> str: r = subprocess.run(cmd, shell=True, capture_output=True, text=True) return r.stdout.strip() def check_disk(threshold=80): for line in sh("df -h --output=pcent,target").splitlines()[1:]: pct, mount = line.split() if int(pct.rstrip("%")) > threshold: print(f"[告警] {mount} 使用率 {pct}") def check_load(): cores = os.cpu_count() load = float(sh("cat /proc/loadavg").split()[0]) status = "告警" if load > cores else "正常" print(f"[{status}] load {load} / {cores} cores") def check_mem(): total, _, free, _, _, avail = sh("free -b").splitlines()[1].split()[1:] print(f"[信息] 可用内存 {int(avail)/2**30:.1f}G") if __name__ == "__main__": check_disk(); check_load(); check_mem() 零第三方依赖,拷到任何有 Python3 的机器就能跑。 ...

2024-06-15 · 3 min · zy

Docker 网络模式详解

前言 容器一多,问题就从"跑起来"变成"怎么连":容器之间怎么互访?端口映射的原理是什么?为什么生产推荐自定义网络?这篇把 Docker 网络讲透。 ...

2024-06-01 · 4 min · zy

Linux 防火墙:firewalld 与 iptables

前言 Linux 防火墙经历了 iptables → nftables → firewalld(前端)的演进。现代 RHEL 系默认 firewalld(底层实为 nftables),但老机器和容器网络里 iptables 依旧无处不在。两个都要会。 ...

2024-05-18 · 3 min · zy

Golang 并发编程:goroutine 与 channel

前言 Go 从语言层面为并发而生:go 关键字一行起"线程",channel 让协程间安全通信。口号是——不要通过共享内存来通信,而要通过通信来共享内存。 ...

2024-05-04 · 4 min · zy

Python 异常处理与文件操作

前言 程序出错是常态,文件处理是日常。这两块写优雅了,脚本的质量直接上一个档次。 一、异常基础 try: result = 10 / int(user_input) except ValueError as e: # 精确捕获, 别裸 except! print(f"输入不是数字: {e}") except ZeroDivisionError: print("除数不能为零") else: print(f"结果: {result}") # 无异常才执行(放成功逻辑, try 块保持精简) finally: print("总是执行") # 清理逻辑(关连接等) 异常层次结构(理解它就不会乱捕获): ...

2024-04-20 · 3 min · zy

Kubernetes 核心概念入门

前言 单机 Docker 跑十几个容器还行,成百上千个跨几十台机器呢?谁挂了谁来重启?怎么滚动升级不中断服务?Kubernetes(K8s)就是容器世界的"操作系统",负责调度、自愈、扩缩容。 ...

2024-04-06 · 4 min · zy

Linux 性能分析入门:top、vmstat、iostat

前言 “服务器好卡"是运维最常收到的一句话。定位性能瓶颈的标准流程:先看全局(top),再分维度下钻(CPU/内存/IO/网络)。这篇讲前三者的经典工具。 ...

2024-03-16 · 4 min · zy

Dockerfile 编写最佳实践

前言 docker run 只是开始,把应用做成自己可控的镜像才算入门容器化。同样的应用,Dockerfile 写法不同,镜像可以从 1GB 到 20MB——这篇讲怎么写出又小又快又安全的镜像。 ...

2024-03-02 · 3 min · zy

Python 面向对象编程基础

前言 Python 里"一切皆对象",但很多同学写脚本一直用不上自定义类。这篇把 OOP 核心概念讲到位,同时告诉你 Pythonic 的取舍——不为了面向对象而面向对象。 ...

2024-02-17 · 3 min · zy

SSH 安全加固实践

前言 一台公网服务器上线几小时,/var/log/secure 里就会堆满爆破记录。SSH 是大门,这篇是一份可以直接照抄的加固清单。 一、先看看有多少人在撬门 # 今天的爆破尝试 grep "Failed password" /var/log/secure | wc -l # 谁在撬(来源 IP Top10) grep "Failed password" /var/log/secure \ | grep -oE "from [0-9.]+" | sort | uniq -c | sort -rn | head # 用哪些用户名试 grep "Failed password" /var/log/secure | grep -oE "invalid user \S+" | sort | uniq -c | sort -rn | head 看完这些数字,你会立刻想做下面所有事。 ...

2024-02-03 · 3 min · zy