
上QQ阅读APP看书,第一时间看更新
How to do it...
We create an instance of NTPClient and then we call the request() method on it by passing the NTP server address.
Listing 1.11 shows how to print the current time from the internet time server as follows:
#!/usr/bin/env python # Python Network Programming Cookbook,
Second Edition -- Chapter - 1 # This program is optimized for Python 2.7.12
and Python 3.5.2. # It may run on any other version with/without
modifications. 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()
In my machine, this recipe shows the following output:
$ python 1_11_print_machine_time.py Fri Jun 2 16:01:35 2017