Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:22:55+00:00 2026-06-16T21:22:55+00:00

I am impressed by all the things Python can do. What I like to

  • 0

I am impressed by all the things Python can do. What I like to know is if I can implement a Python script that can call a JavaScript function.

The Python code I use is detecting an NFC card and reads the Unique ID. Currently I use a Java applet to interact with an HTML page. I think Python is much lighter and better for this.

What I tried is a simple autobahn script server.py and an index.html file.

In the server.py script I implemented this code but it 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);
}

How can I call this function inside server.py

NFCid(toHexString(card.atr))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:22:57+00:00Added an answer on June 16, 2026 at 9:22 pm

    It would in general be possible to set up a WebSocket connection that ferries data from a Python process running a web(sockets) server to a JavaScript function. However, you’d have to explicitly setup a WebSocket connection from JavaScript and have it connect to the server process. Then, you can pass any data coming in over the WebSocket connection (e.g. from Python) to any JavaScript function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently been playing with javascript, HTML5, chrome extensions, jQuery, and all that good
I am impressed with intel thread building blocks. I like how i should write
I am enormously impressed with the ease of use of XLWT, but there is
I use Beyond Compare (a great program), and was very impressed when it displayed
I am trying to implement a Codeigniter based app on Android via phonegap, all
I'm learning Go and so far very impressed with it. I've read all the
I'm new to WordPress and thoroughly impressed with the platform... That is until it
I've been really impressed by the growing number of software platforms that let outside
I just discovered impure.com and was very impressed by what can be achieved in
So, I've been learning PDO. So far, I am not at all impressed, honestly,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.