2011-08-25
美国人Jon写了个PiP,Python in Php项目,可以实现在php中调用python。Jon 79年出生在新泽西州,毕业于罗切斯特理工,曾于2002年合写 Professional PHP4 Programming 示例:
1. Evaluating Python Code from PHP
Result
test 1 50 60.4
test 2.208 test
2. Python Object Instantiation
# module.py
class TestClass:
def __init__(self, foo):
self.foo = foo
def returnInt(self):
return 1113
def test(self, a, b = 'str'):
return "a = %d, b = %s" % (a, b)
def returnMe(self):
return self
def returnTuple(self):
return (1, "two", 3.0)
def returnList(self):
return [1, "two", 3.0]
def returnDict(self):
d = {}
d['one'] = 1
d['two'] = 2
d['three'] = 3
return d
def p(self, var):
print var
returnInt() . "n";
print $p->test(1, 'bar') . "n";
print $p->foo . "n";
$p->foo = 987;
print $p->foo . "n";
# $copy points to the same object
$copy = $p->returnMe();
print $copy->foo . "n";
$p->foo = 987;
print $copy->foo . "n";
?>
Result
1113
a = 1, b = bar
435
987
987
987
3. Type Conversion
returnTuple());
var_dump($p->returnList());
var_dump($p->returnDict());
$a = array('one' => 1, 2, 3);
$p->p($a);
class Test {
var $member = 'test';
}
$t = new Test();
$p->p($t);
?>
Result
array(3) {
[0]=>
int(1)
[1]=>
string(3) "two"
[2]=>
float(3)
}
array(3) {
[0]=>
int(1)
[1]=>
string(3) "two"
[2]=>
float(3)
}
array(3) {
["three"]=>
int(3)
["two"]=>
int(2)
["one"]=>
int(1)
}
{'1': 3, '0': 2, 'one': 1}
{'member': 'test'}
不过,如果不采用PiP这样的方式,也可以考虑用php的system()和popen(),proc_open()来调用。 如果要执行用户脚本,一定要注意安全。可以用escapeshellarg()和escapeshellcmd()或类似preg_replace(‘/[^a-zA-Z0-9]/’, ‘’, $str)去掉无关字符。
PiP官方页面: http://www.csh.rit.edu/~jon/projects/pip/
如非注明转载, 均为原创. 本站遵循知识共享CC协议,转载请注明来源
FEATURED TAGS
css
vc6
http
automake
linux
make
makefile
voip
乱码
awk
flash
vista
vi
vim
javascript
pietty
putty
ssh
posix
subversion
svn
windows
删除
编译
多线程
wxwidgets
ie
ubuntu
开源
c
python
bash
备份
性能
scp
汉字
log
ruby
中文
bug
msn
nginx
php
shell
wordpress
mqueue
android
eclipse
java
mac
ios
html5
js
mysql
protobuf
apache
hadoop
install
iocp
twisted
centos
mapreduce
hbase
thrift
tutorial
hive
erlang
lucene
hdfs
sqoop
utf8
filter
草原
yarn
ganglia
恢复
scrapy
django
fsimage
flume
tail
flume-ng
mining
scala
go
kafka
gradle
cassandra
baas
spring
postgres
maven
mybatis
mongodb
https
nodejs
镜像
心理学
机器学习
Keras
theano
anaconda
docker
spark
akka-http
json
群论
区块链
加密
抽象代数
离散对数
同余
欧拉函数
扩展欧几里德算法
ES6
node-inspect
debug
win10
vscode
挖矿