Pycsc: a pcsc wrapper for Python
Pycsc is now hosted on alioth. Please navigate there to get the latest release.
Pycsc is a python module which defines a new type of object to access smart cards using the PCSC API. Python 2.2 or higher is required to use pycsc.
A smart card is a plastic card (like credit cards) with a chip embedded on it. They are usually used to store sensitive information like cryptographic keys. For more information you could check out this site.
Pycsc provides a new class that ecapsulates the data (handles,..) used to communicate with a smart card reader. Pycsc is built on top of the open source PCSC implementation available on the Muscle web site.The Muscle resource manager (pcscd) runs on the most common Unix platforms. Pycsc also runs on the native implementation of Windows.
Pycsc does not provide any card-level abstraction. If you use Pycsc you therefore need to know what type of card you are communicating with. You have to know the set of instructions suported by the card (also called APDU commands). The manufacturer of the smart you would like to use should give you the reference manual relevant to your card.
Here is an example of use of Pycsc:
import pycsc
import binascii
card = pycsc.pycsc()
#Print reader name
print "Connected to reader: " + card.status()["ReaderName"]
# Print ATR
print "Card Answer To Reset: " + binascii.b2a_hex(card.status()["ATR"])
binascii.b2a_hex(card.transmit("\x00\xA4\x00\x00\x02\x3F\x00"))
print "APDU sent to card: Select MF"
print "Response: " + binascii.b2a_hex(resp)
print "APDU sent to card: Get Response"
print "Response: " + binascii.b2a_hex(card.transmit("\x00\xC0\x00\x00" + resp[-1
]))
# New in pycsc 0.0.3: : support for getStatusChange
readerName = pycsc.listReader()[0]
# Shoud return immediately with current state
newState = pycsc.getStatusChange(ReaderStates=[{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}])
# Block until any change of state
newState = pycsc.getStatusChange(ReaderStates=[{'Reader': readerName, 'CurrentState': newState[0]['EventState']}])
The current version of pycsc is 0.0.3. The API is still not completely supported but the most important functions are already there.
Changes for 0.0.3:
- Implements GetStatusChange()
- one setup.py for all platforms
- added minimal pcsc_stringify_error for Win32 (thanks to Arah Leonard)
- calling constructor with no args or "" is now similar (thanks to Arah Leonard)
- defined more constants (thanks to Arah Leonard)
Changes for 0.0.2:
There is a major change between 0.0.1 and 0.0.2: pycsc now uses strings as parameters. This was modified to allow the use of the struct package.
Other changes include:
- modification of the name of the constructor from pycsc.connect() to pycsc.pycsc()
- addition of the listReader function, to list the readers that are available
- possibility to specify the reader name in the constructor
- possibility to define the protocol (T=1 or T=0).
- Added setup_MacOSX.py and setup_Win32.py for support of these platforms. setup.py is now to be used on Linux.
Unfortunately, the functions are still documented, even through the help() function of Python.
You can download pycsc-0.0.3 here.
You can download pycsc-0.0.2 here.
You can download pycsc-0.0.1 here.