php通过xmlrpc和python通信

server.py

# -*- coding: UTF-8 -*-
import sys,os

#将当前运行目录加入到运行时目录中
sys.path.append(os.path.dirname(__file__))
from twisted.web import resource, server,xmlrpc
from twisted.internet import reactor
import xmlrpclib,urllib2

class GFactory():
     def __init__(self):
         pass
     def recv_rpcPyPut(self,data):
         #print 'recv_rpcPyPut',data
         return 'recv_rpcPyPut',data
     def recv_rpcPyGet(self,data):
         #print 'recv_rpcPyGet',data
         return 'recv_rpcPyGet',data
     def getRPCSite(self):
         source = resource.Resource()
         rpc = xmlrpc.XMLRPC()
         rpc.allowNone = False
         rpc.xmlrpc_rpcPyPut = self.recv_rpcPyPut
         rpc.xmlrpc_rpcPyGet = self.recv_rpcPyGet
         source.putChild('RPC2', rpc)
         print 'getRPCSite:',source
         return source
objFactory=GFactory()
reactor.listenTCP(8088, server.Site(objFactory.getRPCSite()))
reactor.run()
index.php

<?
header("Content-type: text/html; charset=utf-8");
include('./xmlrpc.inc');
class GXmlrpc {
	public $client = null;
	public function __construct($config) {
		$this->client = new xmlrpc_client($config['path'], $config['server'], $config['port']);
		$this->client->setDebug(0);
		$this->client->request_charset_encoding='utf-8';
		$this->client->debug=false;		
		#$this->client->debug=true;		
	}

	public static function getInstance($config) {
		return new GXmlrpc($config);
	}

	public function sendRpcGet($data) {
		try {
			$param = new xmlrpcmsg('rpcPyGet',array(php_xmlrpc_encode($data)));
			$result = & $this->client->send($param);
			if(!$result->faultCode()) {				
				$val = $result->value();
				$return = php_xmlrpc_decode($val);
			} else {
				$return = 1;
			}
		} catch (Exception $e) {
			$return = 2;
		}		
		return $return;
	}


	public function sendRpcPut($data) {
		try {
			$param = new xmlrpcmsg('rpcPyPut',array(php_xmlrpc_encode($data)));
			$result = & $this->client->send($param);
			if(!$result->faultCode()) {
				return 0;
			} else {
				return 1;
			}
		} catch (Exception $e) {
			return 2;
		}		
	}
}

$config=array(
	'path'=>'/RPC2', 'server'=>'10.249.23.99', 'port'=>8088
);


$xmlrpc=GXmlrpc::getInstance($config);
$data=date('Y-m-d H:i:s',time());
$re=$xmlrpc->sendRpcGet($data);
var_dump($re);
?>
结果

 

你可能感兴趣的:(PHP,python,xmlrpc)