Python-based time management application with UDP discovery, TCP protocol communication, time sync, and drift monitoring. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36 lines
1021 B
Python
36 lines
1021 B
Python
"""IP 주소 패딩/정규화 유틸리티"""
|
|
|
|
|
|
def pad_ip(ip: str) -> str:
|
|
"""IP 주소 각 옥텟을 3자리로 패딩.
|
|
예: '192.168.0.74' -> '192.168.000.074'
|
|
"""
|
|
parts = ip.split(".")
|
|
padded = []
|
|
for part in parts:
|
|
part = part.strip()
|
|
padded.append(part.zfill(3)[:3])
|
|
return ".".join(padded)
|
|
|
|
|
|
def normalize_ip(ip: str) -> str:
|
|
"""패딩된 IP를 일반 형태로 변환.
|
|
예: '192.168.000.074' -> '192.168.0.74'
|
|
"""
|
|
parts = ip.split(".")
|
|
return ".".join(str(int(p)) for p in parts)
|
|
|
|
|
|
def sized_string(data: str, length: int, encoding: str = "euc-kr") -> str:
|
|
"""문자열을 지정 바이트 길이로 맞춤 (공백 패딩). MS949/EUC-KR 한글 지원."""
|
|
result = ""
|
|
byte_count = 0
|
|
for ch in data:
|
|
ch_bytes = len(ch.encode(encoding, errors="replace"))
|
|
if byte_count + ch_bytes > length:
|
|
break
|
|
result += ch
|
|
byte_count += ch_bytes
|
|
result += " " * (length - byte_count)
|
|
return result
|