Book Image

Python Network Programming Cookbook

By : Dr. M. O. Faruque Sarker
Book Image

Python Network Programming Cookbook

By: Dr. M. O. Faruque Sarker

Overview of this book

<p>Python is an excellent language to use to write code and have fun by prototyping applications quickly. The presence of lots of third-party libraries, also known as batteries, makes it even more easier and faster to prototype an application or to implement a new algorithm. If you are interested in creating the building blocks for many practical web and networking applications that rely on networking protocols then this book is a must-have.<br /><br />This book highlights major aspects of network programming in Python starting from writing simple networking clients, to developing complex screen-scraping and network security monitoring scripts. It creates the building blocks for many practical web and networking applications that rely on various networking protocols. This book presents the power and beauty of Python in solving the numerous real-world tasks in the area of network programming, system and network administration, network monitoring, and web-application development. <br /><br />This book develops your ability to solve a wide range of network programming tasks in Python. We will start by exploring the Python standard library functions to create client/server network and manipulate your local networking resources available under both IPv4 and IPv6. The practical focus continues with creating web and email clients, scraping web pages, fetching information from various websites, and searching for information on the Web such as Amazon, Flickr, and other sites. It further develops your skills to analyze your network security vulnerabilities using advanced network packet capture and analysis techniques.</p>
Table of Contents (16 chapters)
Python Network Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Printing your machine's name and IPv4 address


Sometimes, you need to quickly discover some information about your machine, for example, the host name, IP address, number of network interfaces, and so on. This is very easy to achieve using Python scripts.

Getting ready

You need to install Python on your machine before you start coding. Python comes preinstalled in most of the Linux distributions. For Microsoft Windows operating system, you can download binaries from the Python website: http://www.python.org/download/

You may consult the documentation of your OS to check and review your Python setup. After installing Python on your machine, you can try opening the Python interpreter from the command line by typing python. This will show the interpreter prompt, >>>, which should be similar to the following output:

~$ python 
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. >>> 

How to do it...

As this recipe is very short, you can try this in the Python interpreter interactively.

First, we need to import the Python socket library with the following command:

>>> import socket

Then, we call the gethostname() method from the socket library and store the result in a variable as follows:

>>> host_name = socket.gethostname()
>>> print "Host name: %s" %host_name
Host name: debian6
>>> print "IP address: %s" %socket.gethostbyname(host_name)
IP address: 127.0.1.1

The entire activity can be wrapped in a free-standing function, print_machine_info(), which uses the built-in socket class methods.

We call our function from the usual Python __main__ block. During runtime, Python assigns values to some internal variables such as __name__. In this case, __name__ refers to the name of the calling process. When running this script from the command line, as shown in the following command, the name will be __main__, but it will be different if the module is imported from another script. This means that when the module is called from the command line, it will automatically run our print_machine_info function; however, when imported separately, the user will need to explicitly call the function.

Listing 1.1 shows how to get our machine info, as follows:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter -1 
# This program is optimized for Python 2.7. It may run on any
# other Python version with/without modifications.

import socket

def print_machine_info():
    host_name = socket.gethostname()
    ip_address = socket.gethostbyname(host_name)
    print "Host name: %s" % host_name
    print "IP address: %s" % ip_address

if __name__ == '__main__':
    print_machine_info()

In order to run this recipe, you can use the provided source file from the command line as follows:

$ python 1_1_local_machine_info.py

On my machine, the following output is shown:

Host name: debian6
IP address: 127.0.0.1

This output will be different on your machine depending on the system's host configuration.

How it works...

The import socket statement imports one of Python's core networking libraries. Then, we use the two utility functions, gethostname() and gethostbyname(host_name). You can type help(socket.gethostname) to see the online help information from within the command line. Alternately, you can type the following address in your web browser at http://docs.python.org/3/library/socket.html. You can refer to the following command:

gethostname(...)
    gethostname() -> string 
    Return the current host name. 

gethostbyname(...) 
   gethostbyname(host) -> address 
    Return the IP address (a string of the form '255.255.255.255') for a host.

The first function takes no parameter and returns the current or localhost name. The second function takes a single hostname parameter and returns its IP address.