• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

thrift调取pythonphpgo客户端代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
深度阅读之《Concurrency in Go》发布时间:2022-07-10
下一篇:
使用go-fuse开发一个fuse文件系统发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap