CIDR 문서의 그림 생성 코드다. if __name__ == "__main__": 아래가 자체 테스트다.
# CIDR 문서의 그림을 만든다: 97_cidr_fig1.svg(라우터 X, Y와 네트워크 A, B), fig2.svg(블록이 덮는 범위와 병합)
# 실행: ~/.venvs/vault-plots/bin/python 97_cidr_plot.py (matplotlib 필요)
import os
import matplotlib
matplotlib.use("svg")
import matplotlib.pyplot as plt
from matplotlib import font_manager
from matplotlib.patches import Rectangle, Circle, Ellipse
HERE = os.path.dirname(os.path.abspath(__file__))
PREFIX = "97_cidr"
INK = "#888888"
BLUE, ORANGE, GREEN, PURPLE, GOLD = "#3b82c4", "#d9622b", "#3a9a5b", "#9b59b6", "#c49a1b"
_fonts = {f.name for f in font_manager.fontManager.ttflist}
FONT = next((n for n in ["Apple SD Gothic Neo", "NanumGothic", "Noto Sans CJK KR"] if n in _fonts), "DejaVu Sans")
plt.rcParams.update({
"font.family": FONT, "font.size": 10.5, "axes.unicode_minus": False, "svg.fonttype": "path",
"svg.hashsalt": PREFIX, "text.color": INK, "axes.facecolor": "none", "figure.facecolor": "none",
"savefig.transparent": True,
})
def save(fig, k):
fig.savefig(os.path.join(HERE, f"{PREFIX}_fig{k}.svg"), bbox_inches="tight", metadata={"Date": None})
if os.environ.get("PREVIEW_DIR"):
fig.savefig(os.path.join(os.environ["PREVIEW_DIR"], f"{PREFIX}_fig{k}.png"), bbox_inches="tight", dpi=110,
facecolor="white")
plt.close(fig)
def fig1():
fig, ax = plt.subplots(figsize=(7.4, 3.2))
ax.set_xlim(0, 10); ax.set_ylim(0, 4.2); ax.axis("off")
ax.plot([0.3, 1.6], [3.3, 2.2], color=INK, lw=1); ax.plot([0.3, 1.6], [1.1, 2.2], color=INK, lw=1)
ax.text(0.2, 3.5, "인터넷 쪽", fontsize=10)
for name, x in [("라우터 Y", 2.2), ("라우터 X", 5.0)]:
ax.add_patch(Circle((x, 2.2), 0.62, fc="none", ec=PURPLE, lw=1.6))
ax.text(x, 2.2, name, ha="center", va="center", fontsize=10.5, color=PURPLE)
ax.plot([2.82, 4.38], [2.2, 2.2], color=INK, lw=1.4)
ax.text(4.25, 2.32, "포트 3", ha="right", va="bottom", fontsize=10)
for lab, y, rng, col, port in [("네트워크 A", 3.4, "210.10.0.x ~ 210.10.7.x", BLUE, "포트 1"),
("네트워크 B", 1.0, "210.10.8.x ~ 210.10.15.x", GREEN, "포트 2")]:
ax.plot([5.55, 7.0], [2.2 + (0.3 if y > 2 else -0.3), y], color=INK, lw=1.2)
ax.text(5.85, 2.2 + (0.62 if y > 2 else -0.62), port, fontsize=10, ha="left", va="center")
ax.add_patch(Ellipse((8.3, y), 3.1, 1.05, fc="none", ec=col, lw=1.6))
ax.text(8.3, y + 0.18, lab, ha="center", va="center", fontsize=10.5, color=col)
ax.text(8.3, y - 0.2, rng, ha="center", va="center", fontsize=10, color=col)
save(fig, 1)
def fig2():
fig, ax = plt.subplots(figsize=(7.8, 3.0))
ax.set_xlim(-3.6, 17.2); ax.set_ylim(-1.5, 3.4); ax.axis("off")
for i in range(16):
ax.text(i + 0.5, 3.05, str(i), ha="center", va="center", fontsize=9.5)
ax.text(-0.3, 3.05, "셋째 바이트", ha="right", va="center", fontsize=10)
for x0, bits, col in [(0, "00000xxx", BLUE), (8, "00001xxx", GREEN)]:
ax.plot([x0 + 0.1, x0 + 7.9], [-0.35, -0.35], color=col, lw=1.4)
ax.text(x0 + 4, -0.75, bits, ha="center", va="center", fontsize=10.5, color=col)
ax.text(-0.3, -0.75, "셋째 바이트 (2진수)", ha="right", va="center", fontsize=10)
rows = [(2.2, 0, 8, "210.10.0.0/21 → A", BLUE), (2.2, 8, 8, "210.10.8.0/21 → B", GREEN),
(0.7, 0, 16, "210.10.0.0/20 → X (라우터 Y의 한 줄)", PURPLE)]
ax.text(-0.3, 2.2, "라우터 X", ha="right", va="center", fontsize=10)
ax.text(-0.3, 0.7, "라우터 Y", ha="right", va="center", fontsize=10)
for y, x0, w, lab, col in rows:
ax.add_patch(Rectangle((x0 + 0.05, y - 0.35), w - 0.1, 0.7, fc=col, alpha=0.25, ec=col, lw=1.4))
ax.text(x0 + w / 2, y, lab, ha="center", va="center", fontsize=10.5, color=col)
save(fig, 2)
if __name__ == "__main__":
fig1(); fig2()
# 셋째 바이트 0~7은 앞 5비트가 00000, 8~15는 00001: 그래서 /21로 각각 한 줄이 된다
assert {format(i, "08b")[:5] for i in range(8)} == {"00000"}
assert {format(i, "08b")[:5] for i in range(8, 16)} == {"00001"}
# 0~15는 앞 4비트가 0000으로 같다: /20 한 줄
assert {format(i, "08b")[:4] for i in range(16)} == {"0000"}
print("ALL CHECKS PASSED")