Code from the excellent book Python for System Administrators, slightly reorganized