How to Get the Current Time From the Internet Time Server Using Python

How to Get the Current Time From the Internet Time Server Using Python

In this tutorial, we will be looking at how to get the current time from an NTP server. To begin we will first install ntplib module so as to access the time.

To install ntplib run the command below.

pip install ntplib

Now create a Python file and insert the code below.

import ntplib
from time import ctime

def print_time():
 ntp_client = ntplib.NTPClient()
 response = ntp_client.request('pool.ntp.org')
 print(ctime(response.tx_time))


if __name__ == '__main__':
 print_time()

Now run the script and get the time as shown below.

Tue May  9 16:27:35 2023

There you have it. Thanks for reading. Happy Coding.

Leave a Comment

Your email address will not be published. Required fields are marked *