2 solutions for executing js in Python
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其他模块一样使用即可。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
