Python How To Print Current Time – Many programs rely on the accurate machine time, such as the make command in UNIX.
Your machine time may be different and need synchronizing with another time server in your network.
In order to synchronize your machine time with one of the Internet time servers, you can write
a Python client for that. For this, ntplib will be used. Here, the client/server conversation will
be done using Network Time Protocol (NTP). If ntplib is not installed on your machine, you
can get it from PyPI with the following command using pip or easy_install:
pip install ntplib
Also you can check Python Socket Programming Articles
1: Python Socket Programming How To Create Socket
2: Python Socket How To Get Local Machine IP Address
3: Python Socket How To Get Website IP Address
4: Python Socket How To Connect TCP Client To Server
5: Python Socket How To Convert IPv4 Address
6: Python Socket How To Set Timeout
So now this is the complete code for Python How To Print Current Time
1 2 3 4 5 6 7 8 9 10 11 12 |
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)) print_time() |
We create an instance of NTPClient and then we call the request() method on it by passing the NTP server address.
Run the complete code this will be the result
How it works
Here, an NTP client has been created and an NTP request has been sent to one of the Internet
NTP servers, pool.ntp.org. The ctime() function is used for printing the response.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email