-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp_python.php
More file actions
executable file
·109 lines (88 loc) · 3 KB
/
php_python.php
File metadata and controls
executable file
·109 lines (88 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
//-----------------------------------------------------------
// PPython(PHP and Python).
// (2012-15 http://code.google.com/p/ppython/)
//
// License: http://www.apache.org/licenses/LICENSE-2.0
//-----------------------------------------------------------
define("LAJP_IP", "127.0.0.1"); //Python端IP
define("LAJP_PORT", 20480); //Python端侦听端口
define("PARAM_TYPE_ERROR", 101); //参数类型错误
define("SOCKET_ERROR", 102); //SOCKET错误
define("LAJP_EXCEPTION", 104); //Python端反馈异常
function ppython()
{
//参数数量
$args_len = func_num_args();
//参数数组
$arg_array = func_get_args();
//参数数量不能小于1
if ($args_len < 1)
{
throw new Exception("[PPython Error] lapp_call function's arguments length < 1", PARAM_TYPE_ERROR);
}
//第一个参数是Python模块函数名称,必须是string类型
if (!is_string($arg_array[0]))
{
throw new Exception("[PPython Error] lapp_call function's first argument must be string \"module_name::function_name\".", PARAM_TYPE_ERROR);
}
if (($socket = socket_create(AF_INET, SOCK_STREAM, 0)) === false)
{
throw new Exception("[PPython Error] socket create error.", SOCKET_ERROR);
}
if (socket_connect($socket, LAJP_IP, LAJP_PORT) === false)
{
throw new Exception("[PPython Error] socket connect error.", SOCKET_ERROR);
}
//消息体序列化
$request = serialize($arg_array);
$req_len = strlen($request);
$request = $req_len.",".$request;
//echo "{$request}<br>"; 这是序列化后的请求数据
$send_len = 0;
do
{
//发送
if (($sends = socket_write($socket, $request, strlen($request))) === false)
{
throw new Exception("[PPython Error] socket write error.", SOCKET_ERROR);
}
$send_len += $sends;
$request = substr($request, $sends);
}while ($send_len < $req_len);
//接收
$response = "";
while(true)
{
$recv = "";
if (($recv = socket_read($socket, 1400)) === false)
{
throw new Exception("[PPython Error] socket read error.", SOCKET_ERROR);
}
if ($recv == "")
{
break;
}
$response .= $recv;
//echo "{$response}<br>";
}
//关闭
socket_close($socket);
$rsp_stat = substr($response, 0, 1); //返回类型 "S":成功 "F":异常
$rsp_msg = substr($response, 1); //返回信息
//echo "返回类型:{$rsp_stat},返回信息:{$rsp_msg}<br>";
if ($rsp_stat == "F")
{
//异常信息不用反序列化
throw new Exception("[PPython Error] Receive Python exception: ".$rsp_msg, LAJP_EXCEPTION);
}
else
{
if ($rsp_msg != "N") //返回非void
{
//反序列化
return unserialize($rsp_msg);
}
}
}
?>