2 Solutions for Python to execute js 1st solutions
SpiderMonkey is part of the Mozilla project and is a JavaScript script engine implemented in C language. it analyzes, compiles, and executes scripts, memory allocation and release operations are performed based on JS data types and object needs. using this engine can give your applications the ability to interpret JavaScript scripts.
To use spidermonkey, you must install it as follows:
Cd/home/linuxany.com/
Wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz-O-| tar xvz
Cd js/src
Make-f Makefile. ref
Mkdir-p/usr/include/smjs/-v
Cp *. {h, tbl}/usr/include/smjs/-v
Cd Linux_All_DBG.OBJ
Cp *. h/usr/include/smjs/-v
Mkdir-p/usr/local/{bin, lib}/-v
Cp js/usr/local/bin/-v
Cp libjs. so/usr/local/lib/-v
After the above installation is complete, run/usr/local/bin/js to start the js interpretation engine.
Python example:
# Coding: utf-8import osimport tempfiledef call_js (js): f = tempfile. mktemp ('SD', 'linuxany', '/tmp') f2 = tempfile. mktemp ('SD', 'linuxany', '/tmp') fp = open (f, 'w') fp. write (js) fp. close () cmd = "/usr/local/bin/js % s> % s" % (f, f2) OS. system (cmd) result = open (f2 ). read () print resultif _ name _ = "_ main _": code = '''function dF (s, n) {n = parseInt (n ); var s1 = unescape (s. substr (0, n) + s. substr (n + 1, s. length-n-1 )); Var t = ''; for (var I = 0; I 2nd solutions Python-Spidermonkey this Python module allows Javascript-related functions to be executed and serves as a bridge between operations between python and javascript, javascript classes, objects, and functions can all be called in Python. It draws a lot of reference from the JavaScript Perl module of colas Jacob bssen, which is based on the combination of Mozilla's PerlConnect Perl. Install: svn checkout http://python-spidermonkey.googlecode.com/svn/trunk/ python-spidermonkey-read-only after downloading, first run python setup. py build and then run python setup. py install official website: checkout. After installation, you can use python in the same way as other modules.