Prev - Index

Python Examples

A MD5 tool

This first example displays the MD5 digest for each file specified on the command line, in a similar fashion to Linux's md5sum and *BSD's md5 programs. The MD5 message digest is a 128-bit fingerprint of a file - acting as a check against accidental (or deliberate) corruption of a file.

#!/usr/bin/env python
# -*- python -*-
# md5er - print the MD5 digest for each specified file
# Written by Graeme Cross <>
# 1 March 1999

import string
import sys
import md5

# the list of hexadecimal characters
hexStr = string.hexdigits

# a function to turn a string of non-printable characters into a string of
# hex characters
def hexify(str):
    r = ''
    for ch in str:
        i = ord(ch)
        r = r + hexStr[(i >> 4) & 0xF] + hexStr[i & 0xF]
    return r
# hexify()

# the main script
if len(sys.argv) < 2:                   # make sure we have specified files
    print "\nSyntax: md5er file(s)\n"

for file in sys.argv[1:]:               # process each file
    m =
    f = open(file, 'r')
    for line in f.readlines():
    md5sum = hexify(m.digest())

    # this matches the output from the FreeBSD md5 program
    print "MD5 (%s) = %s" % (file, md5sum)

# eof

The output is:

[~/mp3/Dance]$ md5er *

MD5 (BangOn.mp3) = ccbe0525600ec9f7d28eeef53518e89b
MD5 (BlockRockinBeats.mp3) = 2933e0afd5155825391e9699a353285f
MD5 (DeadEyesOpened.mp3) = 37015a9afe47e7ac2f950b63a2273bd0
MD5 (Fall.mp3) = dafef48357e4e2e14b709d2dab1c5120
MD5 (HalyconOnOn.mp3) = e3b2b905b60f585504f2645c43f7e754
MD5 (LittleFluffyClouds.mp3) = 9ebd158eb69feaa20536fb359c2bbda1
MD5 (LostInTheKHole.mp3) = d222af82764891d8266339a16d30cec6
MD5 (OnHerMajestysSecretService.mp3) = 26646f9722ae8edb3252ae79509161e0
MD5 (PhoebusApollo.mp3) = 131a8028e71c8d3bce3399a691bf1cf5
MD5 (SettingSun.mp3) = 9de63f08f2a23470a9931eba64728f79
MD5 (SomethingGood.mp3) = 4b5d34246b3750ba17968225afd0298c
MD5 (SweetnessAndLight.mp3) = 765dd1debc4fbfb052cbe20fb2e8f3ad
MD5 (VelvetPants.mp3) = e736182efac6c29139b5f5d9141207fd
MD5 (VoodooPeople.mp3) = 47c8958e68851ce78c6212dcd9fd8e62
MD5 (WhereDoIBegin.mp3) = bbdf0608549fe1a568c3c2d2b27ab623

A USENET group subject reporter

The following script connects to a defined newserver, grabs the headers for the comp.os.linux.announce newsgroup and then displays the Subject line for the last 10 news items.

Note that the NNTP connection is an object, the use of indentation and the use of exceptions to gracefully handle errors when the news server is not defined or when there is a network error.

#!/usr/bin/env python
# Connect to the news server defined in the NNTPSERVER environment variable
# and show the 10 most recent headings from the c.o.l.a newsgroup
# (Adapted from an example in the Python library reference)

import os
from nntplib import NNTP
import socket


    newsServer = os.environ['NNTPSERVER']

except KeyError:

    print "Warning: NNTPSERVER is not defined - trying localhost"
    newsServer = "localhost"

    news = NNTP(newsServer)
    response, count, first, last, name ="comp.os.linux.announce")

    print name, "has", count, "articles. Range =", first, "to", last

    response, subs = news.xhdr('subject', first + '-' + last)

    for id, subject in subs[-10:]:
        print "%6s - %s" % (id, subject)

except socket.error, message:

    print "Whoops - hit an error: %s" % (message)

# eof

And the output is:

[/usr/home/graeme/work/]$ python
comp.os.linux.announce has 311 articles. Range = 12351 to 12824

 12815 - LOCAL: LUG at NCSU Meeting 3/2
 12816 - Doc Toolkit 1.0.0 - E-Text Tools for Palm Computing Platform Users
 12817 - Linux Newbie Administrator FAQ
 12818 - WWW: CableModemInfo.Com - Cable Modem and Linux Info
 12819 - Cxref - C program cross-referencing & documentation tool
 12820 - auswert 0.2 - time code utility for psychological research
 12821 - EJBOSS an EJB application server
 12822 - WWW: call for articles
 12823 - Open Source Erlang 47.4.1 released
 12824 - COMMERCIAL: Small Linux, Embedded Linux CD-ROM available

Prev - Index