PCSC sample in Python using python-pcsclite

Here is a new PCSC sample in Python language for the series PC/SC sample in different languages.

I already presented, pyscard, a Python wrapper in "PCSC sample in Python". This wrapper is a different implementation and API.

Installation

The installation is easy on a Debian system. A .deb package is provided (but the project is not part of Debian).
Or you can rebuild the software from the source code. The current version is 0.13.

The web site is http://python-pcsclite.sourceforge.net/. The license is GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Source code

#!/usr/bin/env python

import pcsclite
import binascii

SELECT = "\x00\xA4\x04\x00\x0A\xA0\x00\x00\x00\x62\x03\x01\x0C\x06\x01"
COMMAND = "\x00\x00\x00\x00"

try:
    context = pcsclite.Context()
    readers = context.list_readers()
    print "PCSC readers:", readers
    reader = readers[0]
    print "Using reader:", reader

    card = context.connect(reader)

    data = card.transmit(SELECT)
    print binascii.b2a_hex(data)

    data = card.transmit(COMMAND)
    print data
    print binascii.b2a_hex(data)

    card.disconnect()

    del card
    del context

except Exception, message:
    print "Exception:", message

Output

$ ./sample_pcsclite.py 
PCSC readers: ('Gemalto PC Twin Reader 00 00',)
Using reader: Gemalto PC Twin Reader 00 00
9000
Hello world!?
48656c6c6f20776f726c64219000

Comments

The python-pcsclite project started in 2007 according to the ChangeLog.txt file. I discovered the project only very recently and open some bugs to make it build on my Debian system. Russell Stuart, the maintainer, was very fast to fix the issues.

Compared to pyscard

python-pcsclite does not build on Mac OS X (see bug #5). I guess it also does not build on Windows but I have not tested it myself. So if you want a Python wrapper for GNU/Linux, Mac OS X and Windows you should use pyscard instead.

python-pcsclite may provide good ideas that I could reuse in pyscard :-)

As for pyscard, Python3 is not yet supported.

History

The python-pcsclite project started in 2007. At the same time the pyscard project is made public (according to the dates in the subversion repository but I guess the pyscard project was already developed since some time).

According to the python-pcsclite web site:
The obvious question is why use python-pcsclite instead of the official one. Python-pcsclite is a fairly direct implementation of C API provided by pcsclite, so direct the documentation for pcsclite applies to python-pcsclite. Pyscard on the other hand builds on pcsclite to provide it's own abstractions. I suspect the choice is more a matter of taste, and being an old C programmer I prefer the directness of the C API, which python-pcsclite emulates.
You have the freedom to select the wrapper you want to use. Compare the sample code above with the 2 examples at my previous article PCSC sample in Python to select the implementation you prefer.

Conclusion

I have not seen any advertising of this project on the Muscle mailing list. The project works fine (as far as I tested) and may benefit to be used by more users.