Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Network Programming Cookbook

You're reading from   Python Network Programming Cookbook Practical solutions to overcome real-world networking challenges

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781786463999
Length 450 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Dr. M. O. Faruque Sarker Dr. M. O. Faruque Sarker
Author Profile Icon Dr. M. O. Faruque Sarker
Dr. M. O. Faruque Sarker
Gary Berger Gary Berger
Author Profile Icon Gary Berger
Gary Berger
Pradeeban Kathiravelu Pradeeban Kathiravelu
Author Profile Icon Pradeeban Kathiravelu
Pradeeban Kathiravelu
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Sockets, IPv4, and Simple Client/Server Programming FREE CHAPTER 2. Multiplexing Socket I/O for Better Performance 3. IPv6, Unix Domain Sockets, and Network Interfaces 4. Programming with HTTP for the Internet 5. Email Protocols, FTP, and CGI Programming 6. Programming Across Machine Boundaries 7. Working with Web Services – XML-RPC, SOAP, and REST 8. Network Monitoring and Security 9. Network Modeling 10. Getting Started with SDN 11. Authentication, Authorization, and Accounting (AAA) 12. Open and Proprietary Networking Solutions 13. NFV and Orchestration – A Larger Ecosystem 14. Programming the Internet

Printing the current time from the internet time server

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.

Getting ready

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
  

If pip is not installed on your computer, first install it before executing the preceding command. In Debian-based Linux distributions such as Ubuntu, this can be installed by:

$ sudo apt install python-pip
  

Note that you will need to install pip for Python 3 separately if you are running it along side Python 2, as typically Python 2 is set as the default version:

$ sudo apt-get install python3-pip
  

Similarly, ntplib needs to be installed for python3-pip (also called pip3) separately:

$ pip3 install ntplib  

It is a good idea to upgrade pip to the latest version if you are running an outdated version, by issuing the following command:

$ pip install --upgrade pip  

or:

$ pip3 install --upgrade pip  

If Python 2 and Python 3 are installed alongside in your computer then use pip3.

I am using the pip version 9.0.1, for both Python 2 and Python 3. This is the latest version at the time of writing.

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
  

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.

You have been reading a chapter from
Python Network Programming Cookbook - Second Edition
Published in: Aug 2017
Publisher: Packt
ISBN-13: 9781786463999
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime