2 solutions for executing js in Python

大家讲道理
Release: 2016-11-08 10:12:41
Original
1452 people have browsed it

The first option

SpiderMonkey is part of the Mozilla project and is a JavaScript script engine implemented in C language. The engine analyzes, compiles and executes scripts, and performs memory allocation and release operations according to the needs of JS data types and objects; use This engine gives your application the ability to interpret JavaScript scripts.

To use spidermonkey, you must install it first. The method is 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 completed, run /usr/local/bin/js and you should be able to start the js interpretation running engine.

Python usage example:

# coding:utf-8
import os
import tempfile
def 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 result
if __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第2种方案Python-Spidermonkey 这个Python模块允许执行Javascript相关功能,是python与javascript之间进行操作的桥梁,javascript的类,对象和函数都可以在Python中调用。它大量借鉴了克拉斯Jacobssen的JavaScript Perl模块,而这又是Mozilla的PerlConnect Perl的结合为基础。安装:svn checkout http://python-spidermonkey.googlecode.com/svn/trunk/ python-spidermonkey-read-only下载完后,先运行python setup.py build然后运行python setup.py install官方网站:http://code.google.com/p/python-spidermonkey/同时需要安装Pyrex模块,一个支持python和C语言混编的模块。装完后就用python其他模块一样使用即可。
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!