golang

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "git.apache.org/thrift.git/lib/go/thrift"
  6. "net"
  7. "thriftproxy"
  8. "time"
  9. )
  10.  
  11. type ThriftClient struct {
  12. client *thriftproxy.ThriftProxyClient
  13. transport *thrift.TSocket
  14. }
  15.  
  16. func (c *ThriftClient) NewThriftClient() {
  17. //thrift
  18. transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
  19. protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
  20. var er error
  21. c.transport, er = thrift.NewTSocket(net.JoinHostPort(thrift_host, thrift_port))
  22. c.transport.SetTimeout(TIMEOUT * time.Second)
  23. if er != nil {
  24. panic(fmt.Sprintf("error resolving address:%v", er))
  25. }
  26. useTransport := transportFactory.GetTransport(c.transport)
  27. c.client = thriftproxy.NewThriftProxyClientFactory(useTransport, protocolFactory)
  28. if err := c.transport.Open(); err != nil {
  29. panic(fmt.Sprintf("Error opening socket:%v", err))
  30. }
  31.  
  32. }
  33. func (c *ThriftClient) Close() {
  34. c.transport.Close()
  35. }
  1. thriftclient := new(ThriftClient)
  2. thriftclient.NewThriftClient()
  3. defer thriftclient.Close()
  4.  
  5. detailRequest := new(se.DetailRequest)
  6. detailRequest.HotelId =178236
  7.  
  8. r, err = thriftclient.client.SearchDetailRtsSync(detailRequest)

php

  1. ?php
  2. header ( "Content-type: text/html; charset=utf-8" );
  3. $GLOBALS['THRIFT_ROOT'] =dirname(__FILE__). '/Thrift';
  4. require_once dirname(__FILE__).'/Thrift.php';
  5. require_once $GLOBALS['THRIFT_ROOT'].'/Protocol/TBinaryProtocol.php';
  6. require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TSocket.php';
  7. require_once $GLOBALS['THRIFT_ROOT'].'/Transport/THttpClient.php';
  8. require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TBufferedTransport.php';
  9. require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TFramedTransport.php';
  10. require_once $GLOBALS['THRIFT_ROOT'].'/Type/TType.php';
  11. require_once $GLOBALS['THRIFT_ROOT'].'/Type/TMessageType.php';
  12. require_once $GLOBALS['THRIFT_ROOT'].'/Factory/TStringFuncFactory.php';
  13. require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/TStringFunc.php';
  14. require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/Core.php';
  15. require_once $GLOBALS['THRIFT_ROOT'].'/Base/TBase.php';
  16. require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TException.php';
  17. require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TProtocolException.php';
  18. require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TTransportException.php';
  19. require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TApplicationException.php';
  20. //error_reporting(E_NONE);
  21.  
  22. $GEN_DIR = './gen-php';
  23. require_once $GEN_DIR.'/ThriftProxy.php';
  24. require_once $GEN_DIR.'/Types.php';
  25. error_reporting(E_ALL);
  26.  
  27. $socket = new Thrift\Transport\TSocket('127.0.0.1', 5101);
  28. $socket->setDebug(true);
  29. // 设置接收超时(毫秒)
  30. $socket->setSendTimeout(10000);
  31. $socket->setRecvTimeout(20000);
  32. $transport = new Thrift\Transport\TFramedTransport($socket);//支持的数据传输方式 取决于服务器端的使用模式 和服务器的设置一样
  33. $protocol = new Thrift\Protocol\TBinaryProtocol($transport); //支持的传输格式 选择传输层,这块要和服务器的设置一样
  34. $client = new ThriftProxyClient($protocol);
  35.  
  36. $transport->open();
  37. try{
  38. $ListRequest=new ListRequest();
  39. $ListRequest->check_in_date= strtotime('2014-07-20');
  40. $ListRequest->check_out_date=strtotime('2014-07-21');
  41. $ListRequest->region_id='178236';
  42. $ListRequest->rank_type=RankType::PRICEASC;
  43. $ListRequest->hotel_star=array(HotelStarType::STAR4,HotelStarType::STAR3);
  44.  
  45. $PageInfo=new PageInfo();
  46. $PageInfo->page_no=1;
  47. $PageInfo->page_size=50;
  48. $ListRequest->page_info=$PageInfo;
  49.  
  50. $a = $client->SearchList($ListRequest);
  51. var_dump($a);
  52. } catch (TException $tx) {
  53. print 'TException: '.$tx->getMessage()."/n";
  54. }
  55. $transport->close();
  56. ?>

python

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import sys
  4. sys.path.append('gen-py')
  5. sys.path.append('thrift')
  6. from thriftproxy import ThriftProxy
  7. from se.ttypes import *
  8.  
  9. from thrift import Thrift
  10. from thrift.transport import TSocket
  11. from thrift.transport import TTransport
  12. from thrift.protocol import TBinaryProtocol
  13. import time
  14.  
  15. try:
  16. socket = TSocket.TSocket('127.0.0.1', 5101)
  17. transport = TTransport.TFramedTransport(socket)
  18. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  19.  
  20. client = ThriftProxy.Client(protocol)
  21. transport.open()
  22.  
  23. request=ListRequest()
  24. request.check_in_date=time.time()+86400
  25. request.check_out_date=time.time()+86400*2
  26. request.region_id=178236
  27. response = client.SearchList(request)
  28. print response
  29.  
  30. transport.close()
  31.  
  32. except Thrift.TException, tx:
  33. print "%s" % (tx.message)

thrift 调取 python php go 客户端代码的更多相关文章

  1. 【hbase】使用thrift with python 访问HBase

    HBase 版本: 0.98.6 thrift   版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: Traceback (most recent ...

  2. 使用Thrift让Python和C#可以相互调用

    在聊如何使用Thrift让Python和C#可以互相调用之前,我们先来看看下面的话题. 一.什么是微服务.微服务的特征.诞生的背景.优势和不足 微服务:使用一套小服务来开发单个应用的方式,每个服务运行 ...

  3. 【Python】 http客户端库requests & urllib2 以及ip地址处理IPy

    requests requests是个HTTPClient库,相比于urllib,urllib2等模块比更加简洁易用 ■ get请求 作为示例,讲一下关于requests如何发起并处理一个get请求 ...

  4. Netty学习——服务器端代码和客户端代码 原理详解

    服务器端代码和客户端代码 原理详解:(用到的API) 0.Socket 连接服务器端的套接字 1.TcompactProtocol   协议层2.TFrameTransport   传输层3.THsh ...

  5. Python网络编程常用代码

    服务器端代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # -*- coding: cp936 -*- ...

  6. python实现一个客户端与服务端的通信

    函数介绍 Socket对象方法: 服务端: 函数 描述 .bind() 绑定地址关键字,AF_INET下以元组的形式表示地址.常用bind((host,port)) .listen() 监听TCP,可 ...

  7. HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端

    HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端 发表时间:2020-03-05 1 ...

  8. axis2生成客户端代码

    通过aix2生成客户端代码需要准备相应的包,然后执行命令,步骤如下: 一.所需包准备 下载axis2-1.6.2-bin.zip,解压从lib包中取出 jaxrpc.jar wsdl4j-1.6.2. ...

  9. 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况

    自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...

随机推荐

  1. 关于c++对文件读写的封装

    namespace { UINT_T GetWriteSizeForNoBuf(UINT_T fsize) { UINT_T write_buf_size = ; == ) { write_buf_s ...

  2. POJ 1011 Sticks 【DFS 剪枝】

    题目链接:http://poj.org/problem?id=1011 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  3. 论文翻译:XNOR-Net: ImageNet Classification Using BinaryConvolutional Neural Networks

    目录 Abstract 1 Introduction 2 Related Work 3 Binary Convolutional Neural Network 3.1 Binary-Weight-Ne ...

  4. 最短路问题:迪杰斯特拉算法(Dijsktra)

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  5. Android学习笔记_58_清除手机应用程序缓存

    通过查看手机设置(setting)源代码,发现它里面获取应用大小和缓存大小是通过PackageManager里面的getPackageSizeInfo方法.然而此方法时私有的,因此通过反射调用此方法. ...

  6. 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)

    洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...

  7. CodeForces 501B Misha and Changing Handles(STL map)

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  8. JavaScript:对事件的反应

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. 学习Node.js知识小结

    什么是Node.js 官方解释:Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js使用了一个事件驱动.非阻塞式I/O的模型( Node.js的特性 ...

  10. Flask—05-理解掌握flask数据模型(01)

    数据模型 数据库回顾 分类: 关系型数据库:MySQL.sqlite.… 非关系型数据库:Redis.MongoDB.… 操作: 执行原生SQL语句,每次都需要拼接SQL语句,非常繁琐而且特别容易出错 ...