window 安装thrift

简介

Thrift最初是由Facebook开发的, 后来提交给了Apache基金会将Thrift作为一个开源项目.
当时facebook开发使用它为了解决系统中各系统间大量数据传输通讯以及系统间语言环境不同需要跨平台的特性.

Thrift 是支持跨语言的, c++, java, python, php, ruby, erlang, perl, haskell, c#, javascript, node.js, smalltask, oCaml

使用版本

当前最近版本是:0.10.0

下载地址:http://archive.apache.org/dist/thrift/0.10.0/

这里下载的是 thrift-0.10.0.exe

安装过程

  • 创建thrift home 目录 (D:\develporDir\thrift)
  • 将下载的thrift-0.10.0.exe 重命名为thrift.exe
  • 将thrift.exe 放在thrift home目录下
  • 在环境变量path中添加(D:\develporDir\thrift)

测试


C:\Users\Administrator>thrift -version
Thrift version 0.10.0

C:\Users\Administrator>
客户端需要安装 `thrift` 模块,可以使用以下命令进行安装: ``` pip install thrift ``` 客户端需要实现以下功能: 1. 显示界面,包括选择图片按钮、发送图片按钮、图片预览等控件。 2. 点击选择图片按钮后,弹出文件选择对话框,选择要发送的图片文件。 3. 点击发送图片按钮后,将图片文件名和图片数据通过 thrift RPC 调用发送给服务器。 以下是客户端的代码示例: ```python import sys import os import io import base64 from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QFileDialog from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from rpc import ImageService class MainWindow(QWidget): def __init__(self): super().__init__() # 选择图片按钮 self.select_button = QPushButton('选择图片', self) self.select_button.move(20, 20) self.select_button.clicked.connect(self.select_image) # 发送图片按钮 self.send_button = QPushButton('发送图片', self) self.send_button.move(20, 60) self.send_button.clicked.connect(self.send_image) # 图片预览 self.image_label = QLabel(self) self.image_label.move(150, 20) self.image_label.resize(300, 300) self.image_label.setScaledContents(True) # 保存图片名称 self.filename = '' def select_image(self): options = QFileDialog.Options() options |= QFileDialog.DontUseNativeDialog filename, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Image Files (*.png *.jpg *.bmp)", options=options) if filename: self.filename = os.path.basename(filename) with open(filename, 'rb') as f: data = f.read() image_data = base64.b64encode(data).decode('utf-8') self.image_label.setPixmap(data) def send_image(self): if not self.filename: return # 通过 thrift RPC 调用发送图片数据 transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ImageService.Client(protocol) transport.open() request = { 'filename': self.filename, 'data': self.image_label.pixmap().toImage().bits().tobytes() } client.save_image(request) transport.close() if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_()) ``` 服务器需要安装 `thrift` 和 `Pillow` 模块,可以使用以下命令进行安装: ``` pip install thrift Pillow ``` 服务器需要实现以下功能: 1. 启动 thrift RPC 服务。 2. 实现保存图片的方法,将接收到的图片数据保存到文件中。 以下是服务器的代码示例: ```python import io import base64 import os from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from rpc import ImageService from PIL import Image class ImageHandler(ImageService.Iface): def save_image(self, request): filename = request['filename'] data = request['data'] # 将图片数据写入文件 with open(filename, 'wb') as f: f.write(data) # 打开图片并显示 image = Image.open(io.BytesIO(data)) image.show() print('保存图片成功:{}'.format(filename)) if __name__ == '__main__': handler = ImageHandler() processor = ImageService.Processor(handler) transport = TSocket.TServerSocket('localhost', 9090) transport_factory = TTransport.TBufferedTransportFactory() protocol_factory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, transport_factory, protocol_factory) print('Starting the server...') server.serve() print('done.') ``` 注意,上述代码中的 `rpc` 模块是通过 `thrift` 生成的,需要先编写 `image.thrift` 文件,然后使用 `thrift` 工具生成 Python 代码。 `image.thrift` 文件内容如下: ``` namespace py.rpc struct ImageData { 1: string filename, 2: binary data, } service ImageService { void save_image(1: ImageData request), } ``` 使用以下命令生成 Python 代码: ``` thrift -r --gen py image.thrift ``` 生成的代码会保存在 `gen-py` 目录中。将 `rpc` 目录(包含 `__init__.py` 和 `ImageService.py` 文件)复制到 `gen-py/py` 目录中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值