windows下thrift的使用(python)

windows下thrift的使用(python)

1、下载thrift,下载地址:http://archive.apache.org/dist/thrift/0.9.3/

2、在编写python的thrift代码时,需要先安装thrift module,下载路径:https://pypi.python.org/pypi/thrift/0.9.1

3、安装thrift:在C盘新建一个Thtift文件夹,将下载的thrift-0.9.3.exe重新命名为thrift.exe后放到Thtift文件夹下,将该路径添加到环境变量,检验是否安装成功:在命令行中输入:

<span>windows下thrift的使用(python)</span>

即可以看到安装后的版本信息。

4、thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言、平台的功能。在thrift的IDL中可以定义以下一些类型:基本数据类型,结构体,容器,异常、服务。

thrift脚本:

const string HELLO_YK = “yk”
service HelloWorld {
void ping(),
string sayHello(),
string sayMsg(1:string msg)
}

thrift脚本通过Thrift编辑器生成所要求的python开发语言代码。即:

  <span>windows下thrift的使用(python)</span>

 

  5、Thrift是一个典型的CS结构,客户端和服务端可以使用不同的语言开发。本文以python为例:

PythonServer.py:

import sys
sys.path.append('./gen-py')
 
from helloworld import HelloWorld
from helloworld.ttypes import *

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
 
import socket

class HelloWorldHandler:
  def __init__(self):
    self.log = {}

  def ping(self):
    print "ping()"

  def sayHello(self):
    print "sayHello()"
    return "say hello from " + socket.gethostbyname(socket.gethostname())

  def sayMsg(self, msg):
    print "sayMsg(" + msg + ")"
    return "say " + msg + " from " + socket.gethostbyname(socket.gethostname())

handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1',30303)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "Starting python server..."
server.serve()
print "done!"

  PythonClient.py

import sys
sys.path.append('./gen-py')

from helloworld import HelloWorld
from helloworld.ttypes import *
from helloworld.constants import *

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

try:
  # Make socket
  transport = TSocket.TSocket('127.0.0.1', 30303)

  # Buffering is critical. Raw sockets are very slow
  transport = TTransport.TBufferedTransport(transport)

  # Wrap in a protocol
  protocol = TBinaryProtocol.TBinaryProtocol(transport)

  # Create a client to use the protocol encoder
  client = HelloWorld.Client(protocol)

  # Connect!
  transport.open()

  client.ping()
  print "ping()"

  msg = client.sayHello()
  print msg
  msg = client.sayMsg(HELLO_YK)
  print msg

  transport.close()

except Thrift.TException, tx:
  print "%s" % (tx.message)

 运行结果:

<span>windows下thrift的使用(python)</span>

 

<span>windows下thrift的使用(python)</span>

 

文件下载:

 相关文件以及测试例子代码

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119491.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • kafuka controller控制[通俗易懂]

    kafuka controller控制[通俗易懂]packagecom.chj.cms.controller;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importorg.springframework.beans.fa…

    2022年6月8日
    39
  • ubuntu android studio_android自启动

    ubuntu android studio_android自启动(1)添加头文件:#include(2)在特定驱动结构体中添加early_suspend结构:#ifdefCONFIG_HAS_EARLYSUSPENDstructearly_suspendearly_suspend;#endif(3)在驱动probe函数中注册相关early_suspend结构体:#ifdefCONFIG_HAS_EARLYSUSPEND

    2022年9月18日
    0
  • redis客户端连接工具连接docker里面redis_gbase客户端连接工具

    redis客户端连接工具连接docker里面redis_gbase客户端连接工具Redis客户端连接工具AnotherRedisDesktopManagermac想用到brew的话,地址:https://www.jianshu.com/p/b7b789a2ed2cAnotherRedisDesktopManager为redis可视化工具,真的巨好用呀!!!原文地址:https://blog.csdn.net/huizhou_achao/article/details/108467792下载及安装教程地址:https://github.com/qishibo/An

    2022年9月19日
    0
  • cnzz友盟怎么安装网站统计代码监控网站流量

    cnzz友盟怎么安装网站统计代码监控网站流量

    2021年9月19日
    59
  • 如何安装java-jdk「建议收藏」

    之前第一次安装java-jdk时,因为不太懂,中间出新了很多问题,所以这次安装想记录下。1.进入oracle官网:www.oracle.com2.将Accept前选中,下载jdk,3.打开下载文件夹,双击程序安装4.安装时可以修改路径,这里选择默认路径C:\ProgramFiles\Java\jdk-11.0.25.发现安装完成后并不能使用因为javac.exe在C:\Pr…

    2022年4月5日
    37
  • labelme怎么安装_putty安装教程

    labelme怎么安装_putty安装教程Labelme安装教程(基于anaconda)1.创建anaconda虚拟环境labelmecondacreate-nlabelmepython=3.6完成之后如图所示(由于我已经创建了labelme故这里用labelme1代替)激活环境:condaactivatelabelme执行完这一步会发现运行环境转移到了labelme,如果没有重新创建2.安装labelme所需要的依赖环境安装的时候使用pip或者conda都可以,两者之中有一个不行时尝试使用另一个,我在安装的时

    2022年9月11日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号