Home > Articles > Open Source > Python

Python Libraries

  • Print
  • + Share This

Optional Operational System

The next set of modules implements interfaces to optional operational system features. Keep in mind that these features are not available for all platforms.


The signal module provides mechanisms to access POSIX signals in order to let the programmer set her own signal handlers for asynchronous events.

A good example is the case when it is necessary to monitor the users, checking whether they press CTRL+C to stop the execution of a program. Although Python provides default handlers, you can overwrite them by creating your own.

import signal, sys
def signal_handler(signal, frame):
 print "You have pressed CTRL+C"
 signal.signal(signal.SIGINT, signal.SIG_IGN)
 print "Now, you can\'t stop the script with CTRL+C " 
 "for the next 10 seconds!"
 signal.signal(signal.SIGALRM, alarm_handler)
 while 1:
  print "I am looping"

def alarm_handler(signal, frame):
 print "Now you can leave the program"

signal.signal(signal.SIGINT, signal_handler)
print "Press CTRL+C"
while 1:

Some of the available signals you can use are as follows:

SIGCONT Continue
SIGING Terminal interrupt character
SIGQUIT Terminal Quit character
SIGTERM Termination
SIG_IGN Signal handler that ignores a signal


The socket module provides access to a low-level BSD socket-style network interface.

See Chapter 10, "Basic Network Background," for details.


The select module is used to implement polling and to multiplex processing across multiple I/O streams without using threads or subprocesses. It provides access to the BSD select() function interface, available in most operating systems.

On windows it only works for sockets. On UNIX, it is used for pipes, sockets, files, and so on.

See Chapter 10 for details.


The thread module supports lightweight process threads. It offers a low-level interface for working with multiple threads.

See Chapter 9 for details.


The threading module provides high-level threading interfaces on top of the thread module.

See Chapter 9 for details.


The Queue module is a synchronized queue class that is used in thread programming to move Python objects between multiple threads.

See Chapter 9 for details.


The anydbm module is a generic dbm-style interface to access variants of the dbm database.

See Chapter 8 for details.


The dumbdbm module is a simple, portable, and slow database implemented entirely in Python.

See Chapter 8 for details.


The dbhash module provides a function that offers a dbm-style interface to access the BSD database library.

See Chapter 8 for details.


The whichdb module provides a function that guesses which dbm module (dbm, gdbm, or dbhash) should be used to open a specific database.

See Chapter 8 for details.


The bsddb module provides an interface to access routines from the Berkeley db library.

See Chapter 8 for details.


The zlib module provides functions that allow compression and decompression using the zlib library. The compression that is provided by this module is compatible with gzip.

For more details check out the zlib library home page at http://www.cdrom.com/pub/infozip/lib.


The gzip module offers support for gzip files. This module provides functions that allow compression and decompression using the GNU compression program gzip.

This module has a class named GzipFile that can be used to read and write files compatible with the GNU gzip program. The objects that are generated by this class behave just like file objects. The only exception is that the seek and tell methods aren't part of the standard implementation.

>>> import gzip
>>> gzipfile = gzip.GzipFile("backup.gz")
>>> contents = gzipfile.read()
>>> print contents


The rlcompleter module provides a completion function for the readline module.

The readline module is a UNIX module that is automatically imported by rlcompleter. It uses a compatible GNU readline library to activate input editing on UNIX.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.