Read NFC cards and Autobahn websocket

#1

Hello,

I am starting a NFC project. I a impressed by all the thing python van do. What i like to know if i can implement a python script that can call a JavaScript function.

The python code I use is detecting a NFC card read the SeriaID. Now I use java applet to interact with a a HTML page. I thing Python is much lighter and better to use.

What is try is a simple autobahn script server.py and a index.html file.

In the server.py script i like to implemented this code but is not working…

#! /usr/bin/env python

from sys import stdin, exc_info

from time import sleep

from smartcard.CardMonitoring import CardMonitor, CardObserver

from smartcard.util import *

import sys

from twisted.internet import reactor

from twisted.python import log

from twisted.web.server import Site

from twisted.web.static import File

from autobahn.websocket import WebSocketServerFactory, \

WebSocketServerProtocol, \

listenWS

class EchoServerProtocol(WebSocketServerProtocol):

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 the list of cards
  """
  def update(self, observable, (addedcards, removedcards)):
  	for card in addedcards:
  		print "+Inserted: ", toHexString(card.atr)
  #call javascript function with <toHexString(card.atr)>
  	for card in removedcards:
  		print "-Removed: ", toHexString(card.atr)
  #call javascript function with <toHexString(card.atr)>

try:

  print "Insert or remove a smartcard in the system."
  print "This program will exit in 10 seconds"
  print ""
  cardmonitor = CardMonitor()
  cardobserver = printobserver()
  cardmonitor.addObserver(cardobserver)
  sleep(10)
  # don't forget to remove observer, or the
  # monitor will poll forever...
  cardmonitor.deleteObserver(cardobserver)
  import sys
  if 'win32' == sys.platform:
  	print 'press Enter to continue'
  	sys.stdin.read(1)

except:

  print exc_info()[0], ':', exc_info()[1]

if name == ‘main’:

if len(sys.argv) > 1 and sys.argv[1] == ‘debug’:

log.startLogging(sys.stdout)

debug = True

else:

debug = False

factory = WebSocketServerFactory(“ws://localhost:9000”,

debug = debug,

debugCodePaths = debug)

factory.protocol = EchoServerProtocol

factory.setProtocolOptions(allowHixie76 = True)

listenWS(factory)

webdir = File(".")

web = Site(webdir)

reactor.listenTCP(8080, web)

reactor.run()

In the index file there is a JavaScript function

function NFCid(msg) {

alert(msg);

}

So how can i call this function inside server.py

NFCid(toHexString(card.atr))

Thank you so much to help me.

0 Likes

#2

Hello,

I am starting a NFC project. I a impressed by all the thing python van
do. What i like to know if i can implement a python script that can call
a JavaScript function.

You can publish an event to a topic from Python and receive that event in JS/browser.

Py Script => publish => Autobahn Server => dispatch => JS/Browser

The python code I use is detecting a NFC card read the SeriaID. Now I
use java applet to interact with a a HTML page. I thing Python is much

Are you really doing _Java_ applets?

lighter and better to use.

What is try is a simple autobahn script server.py and a index.html file.

In the server.py script i like to implemented this code but is not working..

You are using blocking code (i.e. "sleep(10)"). Autobahn is based on Twisted, an asynch networking framework. You should not use blocking code (on the main thread).

You have 2 options:

a) implement you NFC reading stuff using non-blocking code
b) put the blocking code into a background thread

How is your NFC reader connected? Via serial (RS232)?

If so, have a look at

https://github.com/tavendo/AutobahnPython/tree/master/examples/wamp/serial2ws

Hope that helps,

--Tobias

···

Am 30.12.2012 14:47, schrieb Wouter van Reeven:

    #! /usr/bin/env python

    from sys import stdin, exc_info
    from time import sleep

    from smartcard.CardMonitoring import CardMonitor, CardObserver
    from smartcard.util import *
    import sys

    from twisted.internet import reactor
    from twisted.python import log
    from twisted.web.server import Site
    from twisted.web.static import File

    from autobahn.websocket import WebSocketServerFactory, \
                                    WebSocketServerProtocol, \
                                    listenWS

    class EchoServerProtocol(WebSocketServerProtocol):

        # 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 the list of cards
    """

    def update(self, observable, (addedcards, removedcards)):
    for card in addedcards:
    print "+Inserted: ", toHexString(card.atr)
    #call javascript function with <toHexString(card.atr)>
    for card in removedcards:
    print "-Removed: ", toHexString(card.atr)
    #call javascript function with <toHexString(card.atr)>

    try:
    print "Insert or remove a smartcard in the system."
    print "This program will exit in 10 seconds"
    print ""
    cardmonitor = CardMonitor()
    cardobserver = printobserver()
    cardmonitor.addObserver(cardobserver)

    sleep(10)

    # don't forget to remove observer, or the
    # monitor will poll forever...
    cardmonitor.deleteObserver(cardobserver)

    import sys
    if 'win32' == sys.platform:
    print 'press Enter to continue'
    sys.stdin.read(1)

    except:
    print exc_info()[0], ':', exc_info()[1]

    if __name__ == '__main__':

        if len(sys.argv) > 1 and sys.argv[1] == 'debug':
           log.startLogging(sys.stdout)
           debug = True
        else:
           debug = False

        factory = WebSocketServerFactory("ws://localhost:9000",
                                         debug = debug,
                                         debugCodePaths = debug)

        factory.protocol = EchoServerProtocol
        factory.setProtocolOptions(allowHixie76 = True)
        listenWS(factory)

        webdir = File(".")
        web = Site(webdir)
        reactor.listenTCP(8080, web)

        reactor.run()

In the index file there is a JavaScript function

    function NFCid(msg) {
    alert(msg);

    }

So how can i call this function inside server.py

NFCid(toHexString(card.atr))

Thank you so much to help me.

0 Likes