Advertisement
Guest User

pyscard issue

a guest
Sep 16th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1. #! /usr/bin/env python
  2. """
  3. Sample for python PCSC wrapper module: Detect card insertion/removal
  4.  
  5. __author__ = "http://www.gemalto.com"
  6.  
  7. Copyright 2001-2012 gemalto
  8. Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
  9. Copyright 2010 Ludovic Rousseau
  10. Author: Ludovic Rousseau, mailto:ludovic.rousseau@free.fr
  11.  
  12. This file is part of pyscard.
  13.  
  14. pyscard is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU Lesser General Public License as published by
  16. the Free Software Foundation; either version 2.1 of the License, or
  17. (at your option) any later version.
  18.  
  19. pyscard is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU Lesser General Public License for more details.
  23.  
  24. You should have received a copy of the GNU Lesser General Public License
  25. along with pyscard; if not, write to the Free Software
  26. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27. """
  28.  
  29. from smartcard.scard import *
  30. import smartcard.util
  31.  
  32. srTreeATR = \
  33.     [0x3B, 0x77, 0x94, 0x00, 0x00, 0x82, 0x30, 0x00, 0x13, 0x6C, 0x9F, 0x22]
  34. srTreeMask = \
  35.     [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
  36.  
  37.  
  38. def printstate(state):
  39.     reader, eventstate, atr = state
  40.     print reader + " " + smartcard.util.toHexString(atr, smartcard.util.HEX)
  41.     if eventstate & SCARD_STATE_ATRMATCH:
  42.         print '\tCard found'
  43.     if eventstate & SCARD_STATE_UNAWARE:
  44.         print '\tState unware'
  45.     if eventstate & SCARD_STATE_IGNORE:
  46.         print '\tIgnore reader'
  47.     if eventstate & SCARD_STATE_UNAVAILABLE:
  48.         print '\tReader unavailable'
  49.     if eventstate & SCARD_STATE_EMPTY:
  50.         print '\tReader empty'
  51.     if eventstate & SCARD_STATE_PRESENT:
  52.         print '\tCard present in reader'
  53.     if eventstate & SCARD_STATE_EXCLUSIVE:
  54.         print '\tCard allocated for exclusive use by another application'
  55.     if eventstate & SCARD_STATE_INUSE:
  56.         print '\tCard in used by another application but can be shared'
  57.     if eventstate & SCARD_STATE_MUTE:
  58.         print '\tCard is mute'
  59.     if eventstate & SCARD_STATE_CHANGED:
  60.         print '\tState changed'
  61.     if eventstate & SCARD_STATE_UNKNOWN:
  62.         print '\tState unknowned'
  63.  
  64.  
  65. try:
  66.     hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
  67.     if hresult != SCARD_S_SUCCESS:
  68.         raise error(
  69.             'Failed to establish context: ' + \
  70.             SCardGetErrorMessage(hresult))
  71.     print 'Context established!'
  72.  
  73.     try:
  74.         hresult, readers = SCardListReaders(hcontext, [])
  75.         if hresult != SCARD_S_SUCCESS:
  76.             raise error(
  77.                 'Failed to list readers: ' + \
  78.                 SCardGetErrorMessage(hresult))
  79.         print 'PCSC Readers:', readers
  80.  
  81.  
  82.         while True:
  83.             readerstates = []
  84.             for i in xrange(len(readers)):
  85.                 readerstates += [(readers[i], SCARD_STATE_UNAWARE)]
  86.  
  87.             print '----- Current reader and card states are: -------'
  88.             hresult, newstates = SCardGetStatusChange(hcontext, 0, readerstates)
  89.             for i in newstates:
  90.                 printstate(i)
  91.             print '----- Please insert or remove a card ------------'
  92.             hresult, newstates = SCardGetStatusChange(
  93.                                     hcontext,
  94.                                     INFINITE,
  95.                                     newstates)
  96.  
  97.             print '----- New reader and card states are: -----------'
  98.             for i in newstates:
  99.                 printstate(i)
  100.  
  101.     finally:
  102.         hresult = SCardReleaseContext(hcontext)
  103.         if hresult != SCARD_S_SUCCESS:
  104.             raise error(
  105.                 'Failed to release context: ' + \
  106.                 SCardGetErrorMessage(hresult))
  107.         print 'Released context.'
  108.  
  109.     import sys
  110.     if 'win32' == sys.platform:
  111.         print 'press Enter to continue'
  112.         sys.stdin.read(1)
  113.  
  114. except error, e:
  115.     print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement