Skip to Main Content
Python Network Programming
book

Python Network Programming

by Abhishek Ratan, Eric Chou, Pradeeban Kathiravelu, Dr. M. O. Faruque Sarker
January 2019
Beginner to intermediate content levelBeginner to intermediate
776 pages
19h 58m
English
Packt Publishing
Content preview from Python Network Programming

How to do it...

You can lazily write a Python script that calls the system ping command-line tool, as follows:

import subprocess 
import shlex 
 
command_line = "ping -c 1 www.google.com" 
args = shlex.split(command_line) 
try: 
      subprocess.check_call(args,stdout=subprocess.PIPE,\ 
stderr=subprocess.PIPE) 
    print ("Google web server is up!") 
except subprocess.CalledProcessError: 
    print ("Failed to get ping.") 

However, in many circumstances, the system's ping executable may not be available or may be inaccessible. In this case, we need a pure Python script to do that ping. Note that this script needs to be run as a superuser or administrator.

Listing 3.2 shows the ICMP ping, as follows:

#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Network Programming for Network Engineers (Python 3)

Python Network Programming for Network Engineers (Python 3)

David Bombal

Publisher Resources

ISBN: 9781788835466Supplemental Content