본문 바로가기
SW 프로그래밍/파이썬

Python NTPlib 사용

by N2info 2022. 7. 21.

설치

 

$> pip install ntplib

 

활용

import ntplib
from datetime import datetime, timezone

# 폰트 지정
font =  cv2.FONT_HERSHEY_PLAIN

try:    
    client = ntplib.NTPClient()
    response = client.request('kr.pool.ntp.org')
    dt = datetime.fromtimestamp(response.tx_time).strftime("%Y-%m-%d %H:%M:%S")
    
except:
    print('Could not sync with time server.')

출력

response.tx_time : 1658336147.7833595
dt : 2022-07-21 01:55:47