Search code examples
pythontimentp

how extract real time form time.gov in python?


I want to show real time in my program from time.gov. I saw ntplib module and this example:

import ntplib
from time import ctime
c = ntplib.NTPClient()
response = c.request('europe.pool.ntp.org', version=3)
ctime(response.tx_time)

but I can't use time.gov instead of 'europe.pool.ntp.org' because time.gov is not a ntp server. Also I saw some java script code in page source. is there a way to extract real time from time.gov in python with or without ntplib?


Solution

  • Use urllib to retrieve

    http://time.gov/actualtime.cgi

    that returns something like this:

    <timestamp time="1433396367767836" delay="0"/>
    

    Looks like microseconds

    >>> time.ctime(1433396367.767836)
    'Thu Jun  4 15:39:27 2015'