Two solutions for Python to execute js

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.