G2Labs Grzegorz Grzęda
Simple NTP client in Python
June 5, 2023
NTP stands for Network Time Protocol and is the backbone of the Internet. Each secure connection needs a timestamp for procise event processing. If we want to utilise NTP in our own projects (e.g. MicroPython running on ESP32), we don’t need to implement the complete protocol. We can use the basic information in the NTP frame to get the current timestamp.
For this, we need to build a NTP request frame of 48 bytes. In the first byte we place
the value of 0x1B
(27) and rest must be zeros. I will present the complete meaning of both the
request and response frames another time.
The operating port for both the server and the client is 123
. Transmission goes through UDP.
After receiving the response from server, we parse the data and extract second-to-last u32
field,
which is the desired timestamp. The last thing to do is convert the timestamp to a meaningfull value,
becasue the NTP server calculates the timestamp as number of seconds from year 1900.
By substracting the equivalent seconds of 2208988800
to year 1970 (unix epoch) we get the proper value.
To get the socket.bind()
to work, we need the local IP address of the machine (but not 127.0.0.1
).
Example
|
|