PPython - 打通python3 + php

一.预备工具

ppython 工具:  http://code.google.com/p/ppython/

(当然也可以到这里下载:http://down.51cto.com/data/801924

 

python3 网站 : http://www.python.org/getit/

python3操作mysql的模块 :  https://github.com/davispuh/MySQL-for-Python-3

(当然也可以到这里下载:http://down.51cto.com/data/801922

 

* ppython工具正如主页介绍的,结合使用PHP和Python两种语言,取其所长、补其所短。php跟python通过socket通讯,实现前台php可直接与python实时通讯,使用python3开发,所以只支持python3。

 

* MySQL-for-Python-3 :操作python3 连接myslq的模块,由于mysqldb还没成功支持python3,故使用该模块

 


#首选安装python3

#下载  Python-3.3.2.tar.bz2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@  test  Python-3.3.2] tar  -xf Python-3.3.2. tar .bz2
[root@  test  Python-3.3.2] cd  Python-3.3.2
[root@  test  Python-3.3.2]. /configure
[root@  test  Python-3.3.2] make  &&  make  instal
[root@  test  Python-3.3.2]ll  /usr/local/bin/python3
lrwxrwxrwx 1 root root 9 May 20 17:38  /usr/local/bin/python3  -> python3.3
[root@  test  Python-3.3.2] # ll /usr/bin/python*
-rwxr-xr-x 2 root root 8304 Jan  9 19:48  /usr/bin/python
lrwxrwxrwx 1 root root    6 May 20 20:00  /usr/bin/python2  -> python
-rwxr-xr-x 2 root root 8304 Jan  9 19:48  /usr/bin/python2 .4
## python3已经安装完成,因为原先有python2.4 所以默认的python是2版
## 如果系统没依赖python2的版本,设置成python3为默认环境python
[root@  test  Python-3.3.2] # rm -f /usr/bin/python ;
[root@  test  Python-3.3.2] # ln -s /usr/local/bin/python3 /usr/bin/python

 


## 安装mysql - python 模块  MySQL-for-Python-3-master.zip

1
2
3
4
[root@  test  Python-3.3.2] # unzip MySQL-for-Python-3-master.zip
[root@  test  Python-3.3.2] # cd MySQL-for-Python-3-master
[root@  test  Python-3.3.2] # python3 setup.py build
[root@  test  Python-3.3.2] # python3 setup.py install

 

 

### 到这里就安装完成了

 


### 测试使用

### 解压缩ppython 会得到三个文件

  • php_python.py  : python 的主控文件,启动该文件会开启一个socket,等待连接处理。

  • php_python.php : php的交互文件,用来跟python通讯的模块文件

  • process.py  : 核心处理文件,该文件处理php端传送过来的命令并分线程进行处理。

 

### 创建一个test.py 内容如下

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python3
#coding=utf-8
import  MySQLdb
def  go() :
     conn = MySQLdb.connect(host = 'localhost' , user = 'root' , passwd = '123456' , db = 'test' ) ;
     cur = conn.cursor();
     sql = "show tables;"
     cur.execute(sql);
     r = cur.fetchall();
                                                                                                                               
     return  r

 

## 创建一个test.php 内容如下:

1
2
3
4
5
<?php header( "Content-Type: text/html; charset=utf-8" );
require_once ( 'php_python.php' );
$res = ppython( "test::go" );
echo  $res ;
?>

 

 

## 先启动 python3 php_python.py

## 就可以测试  php test.php

 

最后附上上面5个文件打包下载 : http://down.51cto.com/data/801924

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值