Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

now can import module from package,add setsockopt #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*__pycache__/
*.py[cod]
1 change: 1 addition & 0 deletions core/.env
@@ -0,0 +1 @@
PHP_PYTHON_SERVICE_PORT=10240
File renamed without changes.
Binary file added core/mod/sp/__pycache__/sp.cpython-36.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions core/mod/sp/sp.py
@@ -0,0 +1,8 @@
import requests

def sp0(url) :
return url + 'zxcv'

if __name__ == "__main__":
print(sp0('tttteest'))
# skskkskksk
38 changes: 19 additions & 19 deletions core/php_python.php
Expand Up @@ -6,26 +6,26 @@
// License: http://www.apache.org/licenses/LICENSE-2.0
//-----------------------------------------------------------

define("LAJP_IP", "127.0.0.1"); //Python��IP
define("LAJP_PORT", 10240); //Python�������˿�
define("LAJP_IP", "127.0.0.1"); //Python端IP
define("LAJP_PORT", 10240); //Python端侦听端口

define("PARAM_TYPE_ERROR", 101); //�������ʹ���
define("SOCKET_ERROR", 102); //SOCKET����
define("LAJP_EXCEPTION", 104); //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
//参数数量不能小于1
if ($args_len < 1)
{
throw new Exception("[PPython Error] lapp_call function's arguments length < 1", PARAM_TYPE_ERROR);
}
//��һ��������Pythonģ�麯�����ƣ�������string����
//第一个参数是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);
Expand All @@ -42,7 +42,7 @@ function ppython()
throw new Exception("[PPython Error] socket connect error.", SOCKET_ERROR);
}

//��Ϣ�����л�
//消息体序列化
$request = serialize($arg_array);
$req_len = strlen($request);

Expand All @@ -53,7 +53,7 @@ function ppython()
$send_len = 0;
do
{
//����
//发送
if (($sends = socket_write($socket, $request, strlen($request))) === false)
{
throw new Exception("[PPython Error] socket write error.", SOCKET_ERROR);
Expand All @@ -64,7 +64,7 @@ function ppython()

}while ($send_len < $req_len);

//����
//接收
$response = "";
while(true)
{
Expand All @@ -84,29 +84,29 @@ function ppython()

}

//�ر�
//关闭
socket_close($socket);

$rsp_stat = substr($response, 0, 1); //�������� "S":�ɹ� "F":�쳣
$rsp_msg = substr($response, 1); //������Ϣ
$rsp_stat = substr($response, 0, 1); //返回类型 "S":成功 "F":异常
$rsp_msg = substr($response, 1); //返回信息

//echo "��������:{$rsp_stat},������Ϣ:{$rsp_msg}<br>";
//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
if ($rsp_msg != "N") //返回非void
{
try {
if (!unserialize($rsp_msg))
if ($rsp_msg)
echo "receive ".$rsp_msg."<br/>\r\n";

//�����л�
//反序列化
return unserialize($rsp_msg);
}
catch (Exception $e){
Expand Down
33 changes: 22 additions & 11 deletions core/php_python.py
Expand Up @@ -10,7 +10,17 @@
# -------------------------------------------------
# 基本配置
# -------------------------------------------------
LISTEN_PORT = 10240 #服务侦听端口

CONF={}
if (os.path.exists('.env')) :
with open('.env','r') as f:
for line in f.readlines():
lineArr = line.split('=')
if len(lineArr) > 1:
CONF[lineArr[0].strip()] = lineArr[1].strip()

LISTEN_PORT = int(CONF['PHP_PYTHON_SERVICE_PORT']) if 'PHP_PYTHON_SERVICE_PORT' in CONF else 10240 #服务侦听端口

CHARSET = "utf-8" #设置字符集(和PHP交互的字符集)


Expand Down Expand Up @@ -41,18 +51,18 @@
# """释放数据库连接的公共函数"""
# pool.release(conn)

import pymysql
#import pymysql

def getConn() :
return pymysql.connect(
host='localhost',
user='demo',
passwd='test',
db='ppython',
port=3306)
#def getConn() :
# return pymysql.connect(
# host='localhost',
# user='demo',
# passwd='test',
# db='ppython',
# port=3306)

def closeConn(conn):
conn.close();
#def closeConn(conn):
# conn.close();

# -------------------------------------------------
# 主程序
Expand All @@ -66,6 +76,7 @@ def closeConn(conn):
print ("-------------------------------------------")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP/IP
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind(('', LISTEN_PORT))
sock.listen(5)

Expand Down
7 changes: 5 additions & 2 deletions core/process.py
Expand Up @@ -190,11 +190,14 @@ def run(self):

#从消息包中解析出模块名、函数名、入参list
modul, func, params = parse_php_req(reqMsg)
modul = 'mod.' + modul

callMod = __import__ (modul)

if (modul not in pc_dict): #预编译字典中没有此编译模块
#检查模块、函数是否存在
try:
callMod = __import__ (modul) #根据module名,反射出module
callMod = __import__ (modul,fromlist=['blah']) #根据module名,反射出module
pc_dict[modul] = callMod #预编译字典缓存此模块
except Exception as e:
print ('模块不存在:%s' % modul)
Expand All @@ -207,7 +210,7 @@ def run(self):
try:
callMethod = getattr(callMod, func)
except Exception as e:
print ('函数不存在:%s' % func)
print ('函数不存在:%s' % func,e)
self._socket.sendall(("F" + "function '%s()' is not exist!" % func).encode(php_python.CHARSET)) #异常
self._socket.close()
return
Expand Down
29 changes: 15 additions & 14 deletions test/essay.php
@@ -1,22 +1,23 @@
<?php

include_once('./php_python.php');
include_once('../core/php_python.php');

class complex {
var $real;
var $imag;
}

for($i=1;$i<=3;$i++){
for($j = 1;$j<=3;$j++){
for($k = 1;$k<=3;$k++){
$ary = [1,$i,$j,$k,$j,$i];
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
for ($k = 1; $k <= 3; $k++) {
$ary = [1, $i, $j, $k, $j, $i];

$res= ppython("essay::sfft",$ary);
echo implode(',',$ary)." ".json_encode($res)."\n";
try {
$res = ppython("essay::sfft", $ary);
} catch (\Throwable $e) {
echo 'err:::=>';
var_dump($e);
die();
}
echo implode(',', $ary) . " " . json_encode($res) . "\n";

}
}
}
}

}

Expand Down
4 changes: 4 additions & 0 deletions test/sp.php
@@ -0,0 +1,4 @@
<?php
require_once('../core/php_python.php');
$res= ppython("sp.sp::sp0",'www.xxxx.com');
print_r($res . "\n");
6 changes: 0 additions & 6 deletions test/test.php

This file was deleted.

14 changes: 0 additions & 14 deletions test/test.py

This file was deleted.