关闭

python服务端 和 php客户端通信一

2429人阅读 评论(2) 收藏 举报

1 通过thrift

但是数据量太大thrift不支持

下载thrift包安装,比如我的是/usr/local/thrift-0.8.0

/usr/local/thrift-0.8.0/lib类库

/usr/local/thrift-0.8.0/test是你需要的教程 可以通过教材方便使用thrift


通信thrift文件

helloworld .thrift

你要写什么格式可以参照  test里面

service HelloWorld{
     string sayHello(1:string content, 2:string xpath)
 }
                                                                              

thrift -gen python xxx.py

thrift -gen php xxx.py

python服务端 。

test.py

import sys
  2 sys.path.append('/var/www/thrift/gen-py')
  3 from helloworld import HelloWorld
  4 from helloworld.ttypes import *
  5 from thrift.transport import TSocket
  6 from thrift.transport import TTransport
  7 from thrift.protocol import TBinaryProtocol
  8 from thrift.server import TServer
  9 import socket
 10 from lxml.html.soupparser import fromstring
 11 
 12 class HelloWorldHandler:
 13     def __init__(self):
 14         self.log = {}
 15 
 16     #def sayHello(self, content, xpath):
 17         #content = """<html><head><title>why len(y) ==< 1</title><script>var y = 1</script></head>sample.<html>"""
 18     def sayHello(self, content, xpath):
 19         root = fromstring(content)
 20         find_text = root.xpath(xpath)
 21         return find_text[0].text
 22 
 23 handler = HelloWorldHandler()
 24 processor = HelloWorld.Processor(handler)
 25 transport = TSocket.TServerSocket('127.0.0.1', 393939)
 26 tfactory = TTransport.TBufferedTransportFactory()
 27 pfactory = TBinaryProtocol.TBinaryProtocolFactory()
 28 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
 29 print 'start server'
 30 server.serve()

test.php

 <?PHP
  2 $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/Thrift';
  3 require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
  4 require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  5 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
  6 require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
  7 require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
  8 
  9 // Your gen-php dir
 10 $GEN_DIR = dirname(__FILE__).'/gen-php';
 11 require_once $GEN_DIR . '/helloworld/HelloWorld.php';
 12 require_once $GEN_DIR . '/helloworld/helloworld_types.php';
 13 
 14 // Set server host and port
 15 $host = "127.0.0.1";
 16 $port = 393939;
 17 
 18 try {
 19 
 20     //Thrift connection handling
 21     $socket = new TSocket( $host , $port, true );
 22     $transport = new TBufferedTransport($socket, 1024, 1024);
 23     $protocol = new TBinaryProtocol($transport);
 24     #echo "<pre>";
 25     #print_r($socket);
 26     #print_r($transport);
 27     #print_r($protocol);
 28     // get our example client
 29     $client = new HelloWorldClient($protocol);
 30     $transport->open();
 31 
 32     #$in = new ThriftTest_Page();
 33     // Get current timestamp from server
 34     $content = file_get_contents("http://www.baidu.com/");
 35     $content = mb_convert_encoding($content, "UTF-8");
 36 //  $content = "<html><head><title>why len(y) ==< 1</title><script>var y = 1</script></head>sample.<html>";
 37 //  die;
 38     $xpath = "//title";
 39     #$in->content = $content;
 40     #$in->xpath = $xpath;
 41 
 42     $return = $client->sayHello($content, $xpath);
 43 //  $return = $client->sayHello($content, $xpath);
 44     echo $return;
 45 
 46     $transport->close(); } catch (TException $tx) {
 49     print 'Something went wrong: '.$tx->getMessage()."\n";
 50 }
 51 ?>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:137202次
    • 积分:3469
    • 等级:
    • 排名:第9939名
    • 原创:214篇
    • 转载:22篇
    • 译文:0篇
    • 评论:9条
    博客专栏