thrift+python+electron-vue

本文档介绍了如何配置thrift环境,创建electron-vue项目,并实现vue与python通过thrift进行通信。步骤包括环境配置,如安装node.js、electron、vue和thrift,创建并启动electron-vue项目,编写thrift接口文件,生成js和python代码,以及在vue项目中引入和使用thrift接口,最后展示了编写测试例子和python thrift服务端代码。
摘要由CSDN通过智能技术生成

thrift

环境配置(node.js的版本一定要一样)

下载node.js(node-v10.16.0-x64)

下载electron

npm install electron -g

下载vue

npm install @vue/cli -g

下载thrift

npm install thrift -g

开始electron -vue项目

利用electron-vue创建一个名为test的项目

vue init simulatedgreg/electron-vue test

在test文件夹下cmd输入:npm install

启动项目:npm run dev

打通vue跟python
生成文件

在项目文件夹下src/rederer中新建文件夹lib,进入lib文件夹

新建接口文件test.thrift,

输入以下内容:

service userService { 

​		string test1(1:string name) 

} 

注意: test1是python thrift服务端代码中的一个方法

利用该文件生成接口文件(注意 运行下面命令必须要配置环境变量)

thrift -out ./ --gen js:node test.thrift

thrift -out ./ --gen py test.thrift

生成两个test文件夹 init_py 和两个js文件

编写vue连同thrift代码

在lib文件夹下

新建common.js

import Vue from 'vue'

class Api {
   
    static importThrift = () => {
   
        var thrift = require('thrift');
        var userService = require('./userService.js');
        var thriftConnection = thrift.createConnection('127.0.0.1', 9000, 10);
        var thriftClient = thrift.
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Thrift is a framework for building cross-language services. It allows you to define data types and service interfaces in a simple definition file, and then generate code in various languages to implement those interfaces. In Python, you can use the Thrift library to create both client and server applications. To use Thrift in Python, you first need to install the Thrift library. You can do this using pip: ``` pip install thrift ``` Once installed, you can start using Thrift in your Python code. Here's a simple example to get you started: 1. Define your data types and service interface in a Thrift IDL file (e.g., `example.thrift`): ``` namespace py example struct MyStruct { 1: required string name 2: optional i32 age } service MyService { MyStruct getStruct(1: string id) } ``` 2. Generate Python code from the IDL file using the `thrift` compiler: ``` thrift --gen py example.thrift ``` 3. Implement the service interface in Python: ```python from example import MyService, ttypes class MyHandler: def getStruct(self, id): # Implementation code goes here return ttypes.MyStruct(name="John", age=25) handler = MyHandler() processor = MyService.Processor(handler) # Run the server transport = TSocket.TServerSocket(port=9090) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) server.serve() ``` 4. Create a client to interact with the server: ```python from example import MyService, ttypes transport = TSocket.TSocket("localhost", 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MyService.Client(protocol) transport.open() struct = client.getStruct("123") print(struct.name) print(struct.age) transport.close() ``` This is just a basic example to give you an idea of how to use Thrift with Python. You can find more details and advanced usage in the Thrift documentation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值