Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

9.1 Getting the Current Time With NTP

While using sockets directly is very powerful, it can get complicated fast. Bare sockets are great for creating simple, fast-running applications, but when we want to interact with other well-known protocols, it is often best for us to go with tried and true libraries. In this lesson, we'll look at how to get the current time from a Network Time Server pool using the Network Time Protocol (NTP). To do that, we'll use the Python ntplib module.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


9.1 Getting the Current Time With NTP

The next cool trick or utility that I wanna show you, has to do with the Network Time Protocol. Now, what exactly is the Network Time Protocol? Well, you can spend a lot of time reading about it over at ntp.org. And basically what the Network Time Protocol is, is a mechanism for determining the absolute current local time out on a web server somewhere, and being able to propagate that over the network, to clients that are connected to it that are retrieving data. Now, quite often, in the last several years, probably ten, more then ten years. The way to go is to, on your local machine, typically whether it's a Mac or a Windows machine or what have you, you can select an option to say you want to sync it with network time servers, so you can manually go and put the time in there. But, you have to kind of keep track of daylight savings time, and there's things in there to be able to handle that. But, the nice thing is to be able to sync it, with a network time server and then, it will also know when you have switched time zones and all sorts of good things like that, which is pretty nice. You can obviously read a lot about the network time protocol here. You can also read about the RFC for the network time protocol that describes the protocol, and the algorithms used to successfully take on this task. And after you're done reading this, you can implement the entire thing with sockets. But, once again, it's not really logical to have to do that. Especially when there are modules and libraries out there, especially, specifically for Python, because that's what we're talking about, to be able to connect to these servers. Retrieve the data very quickly in maybe one or two commands, and be on your way. So, yes, you could do this with sockets, but I'm gonna show you a very cool library module that's going to do it for you. Now, one other thing that I wanna show you, when it comes to the Network Time Protocol, is there are specific servers out there that you can connect to to retrieve this data from. But one of the other cool things that's out there, is this server pool project. So if you click on this and take a look, pool.ntp.org is basically a large virtual cluster of time servers, that you can go to and connect to, and retrieve this information from. And all these network to virtual clusters of servers are working together to be able to give you the most accurate and the quickest response, for trying to retrieve what the time is in your area. So, that's what we're actually going to be using. We're gonna be using the Network, the NTP pool to be able to retrieve our information, and the way that we gonna do that, is by specifying the pool.ntp.org as the end result of where we're trying to connect to, but let's not get too ahead of ourselves. Let's go ahead and see what it takes to build a very simple client that's going to connect to that NTP pool and get some information back. So, I'm gonna create a new file here. I'm gonna save this and call this 6-whattimeisit.py. So this will be a nice little utility that you can use for any number of reasons, but to get the current time, maybe to sync up the time on your local machine or maybe use for some sort of calculation or whatever. But do understand, that in order to use this successfully, you do have to be connected to the Internet, so that you can actually issue requests out to the server and get a response back. Now in the previous lessons, we've been importing socket. But since we're trying to not directly deal with the sockets, we're going to use a library. And what we wanna do, is we wanna import ntplib. Now, if you're not sure if you have this or not, because it may or may not already be on your system, a very simple way is to open a shell and just type Python, and it's gonna take you into this interactive shell and you can try that command. You can say import ntplib, if you hit enter and it works, then great, then you're ready to go. But, if you are not, you can simply use pip, the package installer that I had mentioned before that you're gonna need earlier in the lesson. And one thing to keep in mind though, if you are using Python 2.X. So, say 2.7 or above, you wanna use the pip that's associated with Version 2. If you're using Python 3, make sure you're using the pip that's installed with Version 3. And, one of the easiest ways to figure that out, if your not sure, is you can type in pip, it's gonna give you a list of all the commands, as you can see down here, you can check the Version. So I'm gonna say pip -V. So, this is Python 9.0.1 and it's coming from Python 3.4. Now, as I mentioned before, I do have multiple versions of Python. I do have multiple versions of pip. So, since I'm using Python 2.7, I wanna use the one that's with 2. And I have that alias as pip 2. So, I could do the same thing here. And, you'll see that this is pip 6.0.7 so it's a little bit behind. It could probably use some updating. But this is coming from Python 2.7, so just make sure that you install using the correct version of pip. So that it's installed in the correct location and you would have access to the NTP Lib from your current version of Python. And all you really have to do to install it, is say pip install, in my case I would say pip2, install NTP lib just like that, and I already have this on my machine, so I don't have to do anything. But, if you didn't, it would go out and download it, put it in the correct location for you, and then you'll be up and running, ready to go. So now that I'm back here, I have NTP lib installed, and I'm ready to go. So, let's go ahead and create a very simple function here. I'm gonna say get_current_time. And then, from within here, I'm simply going to create a new instance of an NTP client. So I'm gonna say, my ntp_client is going to be equal to ntplib.NTPClient. Just like that. So this is going to create a client for me, that understands how to connect to NTP servers, to send data and receive data and do all that sort of good stuff for us. Then, I want to issue a request out there, so I'm gonna say ntp_client, and I'm going to say I want to send a request, and I wanna send a request to, and remember from our website. The connection that I wanna use or the place that I wanna go to is, I want to use that pool of servers, which is coming from pool.ntp.org. So, I'm gonna send a request out there and bundled up in that request is a lot of different information. It's actually, the local time on my local machine, as well as some other information to identify myself. As to kinda give a good guesstimate as to where exactly I am, and it's gonna send that data out to the server. And it's gonna verify some things, and it's gonna send that information back to me, giving me the current time. So, since I'm gonna get a response back, let's go ahead and say response is gonna be equal to. So now, that I have a response, let's print it out, let's see what it says. So, let's say print, and we'll say here current time is, and then we'll simply put out the response here. Let's see what that does, so, we'll save that. And then now, all we'll really have to do is we'll just make this executable from the command lines. So let's say our normal business here, and I want to run get current time just like that. We'll go ahead and save this, so now it's a fairly simple application. Now if I was doing this in sockets and I had to read through the RFC and understand all the commands and all the payloads back and forth, as you can see that would be a very labor intensive process. It doesn't mean you can't do it, but doing things like this that is less than a handful of lines is much, much simpler. So let's go ahead and save that, we'll come back here to our command prompt and I'm gonna say, Python, and this is going to be what time is it and I'm gonna hit enter. And I forgot a double equal sign here, so let's go ahead and save that. We'll come back and clear this up, and then we'll go ahead and run this application again, so current time is, well, we got something there. Seems like we got an object back, an object of type NTP stats which is fine, but that's not really doing us any good, so let's try to tweak this a little bit. So, part of the response object that comes back is a property that we could use so this time let's use this property, it's called tx_time. So, this is actually going to give us the current time. Kind of, but let's see what's going on here. So let's go ahead and rerun this. Okay, we're getting something a little bit more legible. Current time is some really big number. What the heck is that number? Well, that number is one of our favorite Unix time formats, which is really the duration of time since January 1st, 1970. Which is great for computers, because computers can deal with that type of thing. But, it's really difficult for humans to read, so how can we tweak this even more? So, that's actually pretty simple. There is actually a nice little piece of functionality that lives in Python. We're gonna say from time we wanna import ctime. So ctime, is a nice little mechanism to be able to translate this time distance from January 1st 1970, and translate it into something a little bit more legible for us. So the way that we're gonna do that is, we're going to use ctime, we're gonna say ctime and we're gonna wrap our response .tx_time and save that. So now, we're gonna come back again and let's try re-running this one more time. Now, we have something a little bit easier to digest. We can see the current time is Tuesday February 28th and 11:01 AM and 38 seconds in the year 2017. Now, that is a little bit easier to use. So now as you can see, in a matter of about a total of ten lines with a few spaces in there. Really the most important pieces of the puzzle are these three lines. So in three lines, we've been able to encapsulate bundling up a request, sending a request off to a fairly complicated protocol server, the network time servers, getting some information back and printing it out. And now we can see exactly what time it is locally on my machine, using a very simple library that wraps up all of this interconnected networkingness known as NTP lib.

Back to the top