python如何为php写后端?


听到这样一种说法:PHP做前端,python做后端。
感觉是PHP写控制器,python写Model层。
在google上搜了半天也没有个答案。
有人说用python写接口,PHP调用。怎么个实现法呢?还望高手指点一下。

python php

轻音女大学生 12 years, 1 month ago

PHP做前端,PYTHON做后端,为啥要这么做呢,没感觉有什么优点。

以下是找来的一段示例,原作者为:http://www.quou.cn/archives/673,用socket连接。

PYTHON代码:

   
  import time
  
import json
import socket
import traceback
import threading
import SocketServer

class Too:
def __init__(self):
print 'Welcome!'

def test(self, args):
return 'xxx:%s' % args;

def error(self, args):
return 'not function!'

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

def handle(self):
while True:
try:
data = self.request.recv(1024)
if not data:
print 'end'
break
data = json.read(data)
res = getattr(self._object, data['func'], 'error')(data['args'])
if not res:
res = ''
res = str(len(res)).rjust(8, '0')+str(res)
self.request.send(res)
except:
print 'error in ThreadedTCPRequestHandler :%s, res:%s' % (traceback.format_exc(), data)

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass

class Server:
def __init__(self, host, port):
self.host = host
self.port = port
ThreadedTCPServer.allow_reuse_address = True

def open(self, object):
ThreadedTCPRequestHandler._object = object
server = ThreadedTCPServer((self.host, self.port), ThreadedTCPRequestHandler)
server.serve_forever()

if __name__ == '__main__':
Server('0.0.0.0', 10009).open(Too())

PHP代码:

   
  <?php
  
class server{

private $_host;
private $_port;
private $_error;
private $_socket;
private $_class;

function __construct($host, $port){
$this->_host = $host;
$this->_port = $port;
$this->_class = '';
$this->_connect();
}

private function _connect(){
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($sock===false){
$this->_error('error socket_create'.socket_strerror(socket_last_error()));
}

$ret = socket_connect($sock, $this->_host, $this->_port);
if($ret===false){
$this->_error('error socket_connect'.socket_strerror(socket_last_error()));
}

$this->_socket = $sock;
}

public function object($class){
$this->_class = $class;
return $this;
}

public function __call($func, $args){
$data = array('class' => $this->_class, 'func' => $func, 'args' => $args);
$data = json_encode($data);
$socket = $this->_socket;

$res = socket_write($socket, $data, strlen($data));
if($res===false){
$this->_error('error socket_write'.socket_strerror(socket_last_error()));
}

$res = socket_read($socket, 1024, PHP_BINARY_READ);
$result = substr($res, 8);
$len = intval(substr($res, 0, 8));

while(true){
if($len != strlen($result)){
$result .= socket_read($socket, 1024, PHP_BINARY_READ);
}else{
break;
}
}

return $result;
}

private function _error($errMsg = ''){
$this->_error = $errMsg;
echo $errMsg;
exit();
}

public function __destruct(){
$socket = $this->_socket;
socket_write($socket, '', 0); //结束
socket_close($socket);
}
}

function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

$start = microtime_float();

$s = new server('127.0.0.1', 10009);
$res1 = $s->object('Too')->test('a', 'b');
$res2 = $s->object('Too')->test('a', 'b');

$end = microtime_float();

echo $res1.$res2.'<br />';

echo 'cost:'.($end-$start);
?>

fucking answered 12 years, 1 month ago

Your Answer