python搭建thirft前期配置
1.下载thirft https://downloads.apache.org/thrift/0.9.3/
2.pip install thrift
3.编写thrift文件(test.thrift)
service userService {
string newtask(1:string name)
string tasks(1:string jsons)
string pwdtrue(1:string pwdnum)
string select_pwd(1:string lcense)
string remove_apscheduler(1:string job_json)
string start_now_apscheduler(1:string path_json)
string del_task(1:string del_json)
}
4.生成thrift代码
thrift --gen js:node test.thrift
thrift --gen py test.thrift
5.将生成的gen-py改为合法命名,引入到你的server.py中
import calendar
import json
import os
import datetime
import sqlite3
import time
import base64
import hashlib
import pyDes
from psutil import net_if_addrs
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from apscheduler.schedulers.background import BackgroundScheduler
from gen_py.test import userService
thirft 绑定端口
Test()是你搭建server的一个类
port = 8000
ip = "127.0.0.1"
# 创建服务端
handler = Test() # 自定义类
# print(handler.kill_myself())
processor = userService.Processor(handler) # userService为python接口文件自动生成
# 监听端口
transport = TSocket.TServerSocket(ip, port) # ip与port位置不可交换
# 选择传输层
tfactory = TTransport.TBufferedTransportFactory()
# 选择传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# 创建服务端
server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
print("start server in python")
server.serve()
print("Done")
vue监听thirft_server的端口
js:
// 列表
var thrift = require('thrift');
var e = require('events');
// 调用win10下thrift命令自动生成的依赖包
var userService = require('../gen-nodejs/userService.js');
var ttypes = require('../gen-nodejs/test_types.js');
var thriftConnection = thrift.createConnection('127.0.0.1', 8000,10);
var thriftClient = thrift.createClient(userService,thriftConnection);
console.log('33333')
thriftConnection.on("error",function(e) {
console.log(e);
});
console.log('33333')
$("#pwd_true").click(function () {
var task_name = $("#card_pwd").val()
console.log(task_name)
var pwdnum ={
'name':task_name
}
pwdnum = JSON.stringify(pwdnum)
thriftClient.pwdtrue(pwdnum,(error, res) => {
if(error) {
console.error(error,'cgshacgacj')
} else {
console.log('后端方法已调用');
res = JSON.parse(res);
console.log(res.res)
if (res.res == 1) {
var mychar = document.getElementById("lcense");
mychar.style.display="none";
}
}
})
})
Electron的main.js配置
const electron = require('electron');
// 控制应用生命周期的模块
const {app} = electron;
// 创建本地浏览器窗口的模块
const {BrowserWindow} = electron;
// 指向窗口对象的一个全局引用,如果没有这个引用,那么当该javascript对象被垃圾回收的
// 时候该窗口将会自动关闭
let win;
function createWindow() {
// 创建一个新的浏览器窗口
win = new BrowserWindow({
width: 400,//窗体宽度
height: 690,//窗体高度
frame: false,//去除菜单和边框
movable:true,//可否移动
resizable:false,//可否缩放
useContentSize: true,
});
// win.loadURL(`file://${__dirname}/html/task/lensenson.html`);
// 并且装载应用的index.html页面
win.loadURL(`file://${__dirname}/html/task/execute_task.html`);
// 打开开发工具页面
win.webContents.openDevTools();
// 当窗口关闭时调用的方法
win.on('closed', () => {
// 解除窗口对象的引用,通常而言如果应用支持多个窗口的话,你会在一个数组里
// 存放窗口对象,在窗口关闭的时候应当删除相应的元素。
win = null;
});
}
// 当Electron完成初始化并且已经创建了浏览器窗口,则该方法将会被调用。
// 有些API只能在该事件发生后才能被使用。
app.on('ready', createWindow);
// 当所有的窗口被关闭后退出应用
app.on('window-all-closed', () => {
// 对于OS X系统,应用和相应的菜单栏会一直激活直到用户通过Cmd + Q显式退出
if (process.platform !== 'darwin') {
app.quit();
}
});
function cinda_html(app) {
app.on('activate', () => {
// 对于OS X系统,当dock图标被点击后会重新创建一个app窗口,并且不会有其他
// 窗口打开
if (win === null) {
createWindow();
}
});
const path=require('path')
let pyProc = null
let pyPort = null
const createPyProc = () => {
// console.log("999999")
// var dirname = "D:\新建文件夹 (2)\\Testrpa\\dist\\win-unpacked\\resources\\app\\app"
let script = path.join(__dirname, 'py', 'thrift_server')
console.log(script)
pyProc = require('child_process').execFile(script)
// sleep(2000)
}
const exitPyProc = () => {
pyProc.kill()
exitseriver()
pyProc = null
pyPort = null
}
function sleep(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}
const exitseriver = () => {
let kill_exe = path.join(__dirname, 'py', 'kill_server')
kill_server = require('child_process').execFile(kill_exe)
sleep(2000)
}
app.on('ready', createPyProc)
app.on('will-quit', exitPyProc)
}
cinda_html(app)
版权声明:本文为qq_41929657原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。