/pyscard/src/smartcard/Examples/scard-api/sample_control.py

https://bitbucket.org/benallard/pyscard · Python · 97 lines · 78 code · 12 blank · 7 comment · 23 complexity · c18da793055b1b6477738f4911ac9526 MD5 · raw file

  1. #! /usr/bin/env python
  2. """
  3. Sample for python PCSC wrapper module: send a Control Code to a card or
  4. reader
  5. __author__ = "Ludovic Rousseau"
  6. Copyright 2007-2010 Ludovic Rousseau
  7. Author: Ludovic Rousseau, mailto:ludovic.rousseau@free.fr
  8. This file is part of pyscard.
  9. pyscard is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU Lesser General Public License as published by
  11. the Free Software Foundation; either version 2.1 of the License, or
  12. (at your option) any later version.
  13. pyscard is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with pyscard; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. """
  21. from smartcard.scard import *
  22. from smartcard.util import toBytes
  23. try:
  24. hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
  25. if hresult != SCARD_S_SUCCESS:
  26. raise error, 'Failed to establish context: ' + SCardGetErrorMessage(hresult)
  27. print 'Context established!'
  28. try:
  29. hresult, readers = SCardListReaders(hcontext, [])
  30. if hresult != SCARD_S_SUCCESS:
  31. raise error, 'Failed to list readers: ' + SCardGetErrorMessage(hresult)
  32. print 'PCSC Readers:', readers
  33. if len(readers) < 1:
  34. raise error, 'No smart card readers'
  35. for zreader in readers:
  36. print 'Trying to Control reader:', zreader
  37. try:
  38. hresult, hcard, dwActiveProtocol = SCardConnect(
  39. hcontext, zreader, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0)
  40. if hresult != SCARD_S_SUCCESS:
  41. raise error, 'Unable to connect: ' + SCardGetErrorMessage(hresult)
  42. print 'Connected with active protocol', dwActiveProtocol
  43. try:
  44. if 'winscard' == resourceManager:
  45. # IOCTL_SMARTCARD_GET_ATTRIBUTE = SCARD_CTL_CODE(2)
  46. hresult, response = SCardControl(hcard, SCARD_CTL_CODE(2), toBytes("%.8lx" % SCARD_ATTR_VENDOR_NAME))
  47. if hresult != SCARD_S_SUCCESS:
  48. raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult)
  49. r = ""
  50. for i in xrange(len(response)):
  51. r += "%c" % response[i]
  52. print 'SCARD_ATTR_VENDOR_NAME:', r
  53. elif 'pcsclite' == resourceManager and not 'pcsclite-tiger' == resourceManagerSubType:
  54. # get firmware on Gemplus readers
  55. hresult, response = SCardControl(hcard, SCARD_CTL_CODE(1), [0x02])
  56. if hresult != SCARD_S_SUCCESS:
  57. raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult)
  58. r = ""
  59. for i in xrange(len(response)):
  60. r += "%c" % response[i]
  61. print 'Control:', r
  62. finally:
  63. hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD)
  64. if hresult != SCARD_S_SUCCESS:
  65. raise error, 'Failed to disconnect: ' + SCardGetErrorMessage(hresult)
  66. print 'Disconnected'
  67. except error, (message):
  68. print error, message
  69. finally:
  70. hresult = SCardReleaseContext(hcontext)
  71. if hresult != SCARD_S_SUCCESS:
  72. raise error, 'Failed to release context: ' + SCardGetErrorMessage(hresult)
  73. print 'Released context.'
  74. except error, e:
  75. print e
  76. import sys
  77. if 'win32' == sys.platform:
  78. print 'press Enter to continue'
  79. sys.stdin.read(1)