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)
thrift 调取 python php go 客户端代码的更多相关文章
- 【hbase】使用thrift with python 访问HBase
HBase 版本: 0.98.6 thrift 版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: Traceback (most recent ...
- 使用Thrift让Python和C#可以相互调用
在聊如何使用Thrift让Python和C#可以互相调用之前,我们先来看看下面的话题. 一.什么是微服务.微服务的特征.诞生的背景.优势和不足 微服务:使用一套小服务来开发单个应用的方式,每个服务运行 ...
- 【Python】 http客户端库requests & urllib2 以及ip地址处理IPy
requests requests是个HTTPClient库,相比于urllib,urllib2等模块比更加简洁易用 ■ get请求 作为示例,讲一下关于requests如何发起并处理一个get请求 ...
- Netty学习——服务器端代码和客户端代码 原理详解
服务器端代码和客户端代码 原理详解:(用到的API) 0.Socket 连接服务器端的套接字 1.TcompactProtocol 协议层2.TFrameTransport 传输层3.THsh ...
- 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 -*- ...
- python实现一个客户端与服务端的通信
函数介绍 Socket对象方法: 服务端: 函数 描述 .bind() 绑定地址关键字,AF_INET下以元组的形式表示地址.常用bind((host,port)) .listen() 监听TCP,可 ...
- HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端
HTTPS请求HTTP接口被浏览器阻塞,python实现websocket客户端,websocket服务器,跨域问题,dwebsocket,https,拦截,服务端 发表时间:2020-03-05 1 ...
- axis2生成客户端代码
通过aix2生成客户端代码需要准备相应的包,然后执行命令,步骤如下: 一.所需包准备 下载axis2-1.6.2-bin.zip,解压从lib包中取出 jaxrpc.jar wsdl4j-1.6.2. ...
- 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况
自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...
随机推荐
- Android(java)学习笔记51:ScrollView用法
1. 理论部分 (1)ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 (2)上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 (3)上述两个控件可以互 ...
- NEUACM1132: Renew MST Quickly 增量最小生成树
题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1132 和UVa11354很类似 题意: 原先有一棵树,每次加一条边,看最小生成树大小: 这个和增量 ...
- 2018.12.1 web项目中解决乱码问题的一个工具类
<!-- 配置一个过滤器 编码格式的过滤器 --> <filter> <filter-name>encodeFilter</filter-name> & ...
- linnx 修改ip地址
vi /etc/sysconfig/network-scripts/ifcfg-eth0 [编辑网卡的配置文件] 输入上述命令后回车,打开配置文件,使用方向键移动光标到最后一行,按字母键“i”,进入编 ...
- 运lucky
运 [问题背景] zhx 和妹子们玩数数游戏. [问题描述] 仅包含 4 或7 的数被称为幸运数. 一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列. 两个子序列被定义为不同的当且 ...
- java 注解annotation的使用,以及反射如何获取注解
一.注解基本知识 1.元注解 元注解是指注解的注解.包括 @Retention @Target @Document @Inherited四种. 1. Annotation型定义为@interfac ...
- neo4j 安装步骤 转自:http://blog.csdn.net/luoluowushengmimi/article/details/19987995
1. Neo4j简介 Neo4j是一个用Java实现的.高性能的.NoSQL图形数据库.Neo4j 使用图(graph)相关的概念来描述数据模型,通过图中的节点和节点的关系来建模.Neo4j完全兼容A ...
- o'Reill的SVG精髓(第二版)学习笔记——第四章
第四章:基本形状 4.1线段 SVG可以使用<line>元素画出一条直线段.使用时只需要指定线段起止点的x和y坐标即可.指定坐标时可以不带单位,此时会使用用户坐标,也可以带上单位,如em. ...
- 2017-09-26 发布 SpringBoot多模块项目实践(Multi-Module)
https://segmentfault.com/a/1190000011367492?utm_source=tag-newest 2017-09-26 发布 SpringBoot多模块项目实践(Mu ...
- 序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
查询窗口中可以设置很多查询条件 表单中输入的内容转为datagrid的load方法所需的查询条件向原请求地址再次提出新的查询,将结果显示在datagrid中 转换方法看代码注释 <td cols ...