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

Reusing socket addresses


You want to run a socket server always on a specific port even after it is closed intentionally or unexpectedly. This is useful in some cases where your client program always connects to that specific server port. So, you don't need to change the server port.

How to do it...

If you run a Python socket server on a specific port and try to rerun it after closing it once, you won't be able to use the same port. It will usually throw an error like the following command:

Traceback (most recent call last):
  File "1_10_reuse_socket_address.py", line 40, in <module>
    reuse_socket_addr()
  File "1_10_reuse_socket_address.py", line 25, in reuse_socket_addr
    srv.bind( ('', local_port) )
  File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

The remedy to this problem is to enable the socket reuse option, SO_REUSEADDR.

After creating a socket object, we can query the state of address reuse, say an old state. Then, we call the setsockopt() method to alter the value of its address reuse state. Then, we follow the usual steps of binding to an address and listening for incoming client connections. In this example, we catch the KeyboardInterrupt exception so that if you issue Ctrl + C, then the Python script gets terminated without showing any exception message.

Listing 1.10 shows how to reuse socket addresses 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
import sys

def reuse_socket_addr():
    sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )

    # Get the old state of the SO_REUSEADDR option
    old_state = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR )
    print "Old sock state: %s" %old_state

    # Enable the SO_REUSEADDR option
    sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
    new_state = sock.getsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR )
    print "New sock state: %s" %new_state

    local_port = 8282
    
    srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    srv.bind( ('', local_port) )
    srv.listen(1)
    print ("Listening on port: %s " %local_port)
    while True:
        try:
            connection, addr = srv.accept()
            print 'Connected by %s:%s' % (addr[0], addr[1])
        except KeyboardInterrupt:
            break
        except socket.error, msg:
            print '%s' % (msg,)

if __name__ == '__main__':
    reuse_socket_e addr()

The output from this recipe will be similar to the following command:

$ python 1_10_reuse_socket_address.py 
Old sock state: 0
New sock state: 1
Listening on port: 8282 

How it works...

You may run this script from one console window and try to connect to this server from another console window by typing telnet localhost 8282. After you close the server program, you can rerun it again on the same port. However, if you comment out the line that sets the SO_REUSEADDR, the server will not run for the second time.