Skip to content

Instantly share code, notes, and snippets.

@zatarra
Created December 16, 2014 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zatarra/75df47c8bd5a8d913cb4 to your computer and use it in GitHub Desktop.
Save zatarra/75df47c8bd5a8d913cb4 to your computer and use it in GitHub Desktop.
Read UID from HID iClass cards
from smartcard.CardType import AnyCardType
from smartcard.CardRequest import CardRequest
from smartcard.util import toHexString, toBytes
from smartcard.CardMonitoring import CardMonitor, CardObserver
from smartcard.util import *
import urllib2
import time
# a simple card observer that prints inserted/removed cards
class printobserver( CardObserver ):
"""A simple card observer that is notified
when cards are inserted/removed from the system and
prints its uids. The code is not pretty but it works!
"""
def update( self, observable, (addedcards, removedcards) ):
apdu = [0xff, 0xca, 0, 0, 0]
for card in addedcards:
try:
cardtype = AnyCardType()
cardrequest = CardRequest( timeout=1, cardType=cardtype )
cardservice = cardrequest.waitforcard()
cardservice.connection.connect()
response, sw1, sw2 = cardservice.connection.transmit(apdu)
tagid = toHexString(response).replace(' ','')
print tagid
#urllib2.urlopen("http://your_web_servers_waiting_for_card_data/?uid=%s" % tagid, None, 3)
except Exception as e:
print "Exception detected: %s" % e
print "Card Monitor started..."
cardmonitor = CardMonitor()
cardobserver = printobserver()
cardmonitor.addObserver( cardobserver )
while True:
time.sleep(3600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment