广告
返回顶部
首页 > 资讯 > 后端开发 > Python >thrift的使用--python
  • 257
分享到

thrift的使用--python

thriftpython 2023-01-31 02:01:42 257人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

转载blog:Http://www.cnblogs.com/pinking/p/7726478.html在这里要补充一点的就是在在这里python要安装thrift包时候,可以直接在安装好的thrift好的模块中sudo Python s

转载blog:Http://www.cnblogs.com/pinking/p/7726478.html


在这里要补充一点的就是在

在这里python要安装thrift包时候,可以直接在安装好的thrift好的模块中sudo Python setup.py install安装就可以,

java、c++的我暂时没走通,周末来摸索


以下原文,作者在windows上面,我在linux上面:

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:

tar -zvxf thrift-0.9.3.tar.gz 直接下一步。。。

:在命令行中输入:

y]$ thrift  --version

Thrift version 0.9.3

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

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

thrift脚本helloworld.thrift:

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

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

thrift  -r  --gen py  helloworld.thrift 

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

PythonServer.py:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
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

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)

运行结果:


server端:

python  PythonServer.py 

Starting python server...

ping()


client端:

python PythonClient.py 

ping()

say hello from 10.101.173.116

say yk from 10.101.173.116







--结束END--

本文标题: thrift的使用--python

本文链接: https://www.lsjlt.com/news/186823.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • thrift的使用--python
    转载blog:http://www.cnblogs.com/pinking/p/7726478.html在这里要补充一点的就是在在这里python要安装thrift包时候,可以直接在安装好的thrift好的模块中sudo python s...
    99+
    2023-01-31
    thrift python
  • 使用python-thrift问题汇总
    使用环境是Centos6.4,python版本2.7.3,thrift版本0.9.0。使用中遇到了以下问题:1. root:code for hash md5 was not found没有找到MD5的库,于是解释器又去寻找SHA1 SH...
    99+
    2023-01-31
    python thrift
  • python实现Thrift服务端的方法
    目录环境准备编写.thrift文件生成python对应的thrift代码编写服务端编写客户端用于测试测试服务端近期在项目中存在跨编程语言协作的需求,使用到了Thrift。本文将记录用...
    99+
    2022-11-12
  • ​thrift是什么及怎么使用
    Thrift是一个软件框架,用于跨语言的服务开发。它由Apache软件基金会开发和维护,旨在帮助开发人员编写高效和可扩展的客户端-服...
    99+
    2023-10-21
    ​thrift
  • C#怎么使用Thrift作为RPC框架
    这篇文章主要讲解了“C#怎么使用Thrift作为RPC框架”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么使用Thrift作为RPC框架”吧!完善开发工具通过nuget在vs2019中...
    99+
    2023-06-21
  • 利用python访问Hbase(Thrift模块安装与测试)
    hadoop环境介绍:master服务:node1slave服务器:node2,node3,node4mysql服务器:node29Thrift安装在node1服务器上!相关软件版本:hadoop版本:ha...
    99+
    2022-10-18
  • hbase之python利用thrift操作hbase数据和shell操作
    前沿:        以前都是用mongodb的,但是量大了,mongodb显得不那么靠谱,改成hbase撑起一个量级。HBase是Apache Hadoop的数据库...
    99+
    2022-10-18
  • C#怎么使用远程服务调用框架Apache Thrift
    这篇“C#怎么使用远程服务调用框架Apache Thrift”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#怎么...
    99+
    2023-07-02
  • C#使用Thrift作为RPC框架入门详细教程
    前言 本文将介绍由 Facebook 开发的远程服务调用框架 Apache Thrift,它采用接口描述语言定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码生成引擎可以在多种...
    99+
    2022-11-12
  • 使用Java如何实现创建一个thrift服务器
    使用Java如何实现创建一个thrift服务器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结...
    99+
    2023-05-31
    java thrift 服务器
  • 使用 Python 的 jsonsche
    在OpenStack中, 使用了Python的 jsonschema包, 对json字符串做了验证. Python JSON Schema Library https://pypi.python.org/pypi/jsonschema...
    99+
    2023-01-31
    Python jsonsche
  • python+etcd的使用
    python-etcd是一个etcd的python客户端,它的安装方法如下: git clone https://github.com/jplana/python-etcd.git cd python-etcd python se...
    99+
    2023-01-31
    python etcd
  • python的tkinter使用
    __author__ = 'Python' import tkinter as tk class Application(tk.Frame):     def __init__(self, master=None):         t...
    99+
    2023-01-31
    python tkinter
  • pygrametl的使用--python
    pygrametl是一个python的package用于ETL(Extract-Transform-Load ) 简例 import MySQLdb from pygrametl.datasources import SQLSource ...
    99+
    2023-01-31
    pygrametl python
  • Python webdriver.Chrome()的使用
    前提 Python与Chrome路径下均安装chromedriver.exe。 2.chromedriver.exe版本选择及下载 下载地址为:http://npm.taobao.org/mirror...
    99+
    2023-10-04
    python chrome 开发语言
  • Python中的*使用
    Python中的*使用   在为函数传递参数和函数定义时使用参数的时候,时常会看到有和 *和**,下面分别讲解其作用。 调用函数时使用*和 ** 假设有函数 def test(a, b, c) test(*args):* 的作...
    99+
    2023-01-31
    Python
  • python-fire的使用
    本文完全转载自:https://github.com/google/python-fire/blob/master/docs/guide.md fire简单参考实例:http://blog.csdn.net/u010099080/art...
    99+
    2023-01-31
    python fire
  • Python collection的使用
    Python中的基本数据结构有list,dict,tuple,set。Python还有一个功能比较强大的包collections,可以处理并维护一个有序的dict,可以提高程序的运行效率。 1、collections中defau...
    99+
    2023-01-31
    Python collection
  • Python的@property的使用
    目录1、几个概念2、举个例子3、解决问题4、换个方法通常,当我们需要对对象的敏感属性或者不希望外部直接访问的属性进行私有化,但是某些时候我们又需要对这些私有属性进行修改,该怎么处理呢...
    99+
    2022-11-12
  • python中list的使用
    1、list(列表)是一种有序的集合,可以随时添加、修改、删除其中的元素。 举例:listClassName = ['Jack','Tom','Mark']                     列表可以根据索引获取元素,如:listC...
    99+
    2023-01-30
    python list
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作