One of the most common parts of network debugging and security testing is port checking. Whether your application is supposed to use a given port, as in P2P or server-client relations of different protocols, or whether you are sweeping for an opening on your network, being able to automate the checking of ports on a set of servers is a helpful facility to have.
This tutorial guides you through building a port scanner in Python using three modules: sys, optparse, and socket. The first is for cleanly exiting the program. The second is for enabling options (aka "flags") on the command line. The third is the base level module for network programming in Python. The brains of the program will rely simply on opening and connecting to a socket via TCP.