thrift 调取 python php go 客户端代码

golang

package main

import (
    "fmt"
    "git.apache.org/thrift.git/lib/go/thrift"
    "net"
    "thriftproxy"
    "time"
)

type ThriftClient struct {
    client    *thriftproxy.ThriftProxyClient
    transport *thrift.TSocket
}

func (c *ThriftClient) NewThriftClient() {
    //thrift
    transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
    protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
    var er error
    c.transport, er = thrift.NewTSocket(net.JoinHostPort(thrift_host, thrift_port))
    c.transport.SetTimeout(TIMEOUT * time.Second)
    if er != nil {
        panic(fmt.Sprintf("error resolving address:%v", er))
    }
    useTransport := transportFactory.GetTransport(c.transport)
    c.client = thriftproxy.NewThriftProxyClientFactory(useTransport, protocolFactory)
    if err := c.transport.Open(); err != nil {
        panic(fmt.Sprintf("Error opening socket:%v", err))
    }

}
func (c *ThriftClient) Close() {
    c.transport.Close()
}
    thriftclient := new(ThriftClient)
    thriftclient.NewThriftClient()
    defer thriftclient.Close()

    detailRequest := new(se.DetailRequest)
    detailRequest.HotelId =178236

r, err = thriftclient.client.SearchDetailRtsSync(detailRequest)

 

php

?php  
    header ( "Content-type: text/html; charset=utf-8" );
    $GLOBALS['THRIFT_ROOT'] =dirname(__FILE__). '/Thrift';   
    require_once dirname(__FILE__).'/Thrift.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Protocol/TBinaryProtocol.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TSocket.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/THttpClient.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TBufferedTransport.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TFramedTransport.php';  
    require_once $GLOBALS['THRIFT_ROOT'].'/Type/TType.php'; 
    require_once $GLOBALS['THRIFT_ROOT'].'/Type/TMessageType.php'; 
    require_once $GLOBALS['THRIFT_ROOT'].'/Factory/TStringFuncFactory.php'; 
    require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/TStringFunc.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/Core.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Base/TBase.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TException.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TProtocolException.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TTransportException.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TApplicationException.php';
    //error_reporting(E_NONE);  

    $GEN_DIR = './gen-php';  
    require_once $GEN_DIR.'/ThriftProxy.php';  
    require_once $GEN_DIR.'/Types.php'; 
    error_reporting(E_ALL);  
   
    $socket = new Thrift\Transport\TSocket('127.0.0.1', 5101);  
    $socket->setDebug(true);
    // 设置接收超时(毫秒)  
    $socket->setSendTimeout(10000);
    $socket->setRecvTimeout(20000);
    $transport = new Thrift\Transport\TFramedTransport($socket);//支持的数据传输方式 取决于服务器端的使用模式 和服务器的设置一样 
    $protocol = new Thrift\Protocol\TBinaryProtocol($transport);  //支持的传输格式 选择传输层,这块要和服务器的设置一样 
    $client = new ThriftProxyClient($protocol);  
   
    $transport->open();  
    try{  
        $ListRequest=new ListRequest();
        $ListRequest->check_in_date=  strtotime('2014-07-20');
        $ListRequest->check_out_date=strtotime('2014-07-21');
        $ListRequest->region_id='178236';
        $ListRequest->rank_type=RankType::PRICEASC;
        $ListRequest->hotel_star=array(HotelStarType::STAR4,HotelStarType::STAR3);

        $PageInfo=new PageInfo();
        $PageInfo->page_no=1;
        $PageInfo->page_size=50;
        $ListRequest->page_info=$PageInfo;
        
        $a = $client->SearchList($ListRequest);  
        var_dump($a);  
    } catch (TException $tx) {  
        print 'TException: '.$tx->getMessage()."/n";  
    }
    $transport->close();  
?>

python

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys 
sys.path.append('gen-py')
sys.path.append('thrift') 
from thriftproxy import ThriftProxy   
from se.ttypes import *

from thrift import Thrift 
from thrift.transport import TSocket 
from thrift.transport import TTransport 
from thrift.protocol import TBinaryProtocol
import time

try: 
    socket = TSocket.TSocket('127.0.0.1', 5101)
    transport = TTransport.TFramedTransport(socket)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
                        
    client = ThriftProxy.Client(protocol) 
    transport.open()
    
    request=ListRequest()
    request.check_in_date=time.time()+86400
    request.check_out_date=time.time()+86400*2
    request.region_id=178236
    response = client.SearchList(request) 
    print response
    
    transport.close()
        
except Thrift.TException, tx: 
    print "%s" % (tx.message)

 

posted @ 2014-11-24 14:57  wangxusummer  阅读(843)  评论(0编辑  收藏  举报