thrift JAVA服务端 python客户端的实现

2024-09-01 20:38

本文主要是介绍thrift JAVA服务端 python客户端的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近用Python做网页的抓取,因为想得到JS解释后的HTML,先后尝试了selenium,windmill,htmlunit等web测试框架,因为只要得到html不需要界面展现,最后选择了htmlunit,而htmlunit只有Java的实现,所以考虑用RPC来进行python与JAVA的连接

最开始试用了一下ICE,JAVA端无问题,在用python做client的时候,发现ICE现在还不支持python2.7,放弃,再来看看thrift

下载地址http://thrift.apache.org/download/


先编写一个IDL接口定义

demo.thrift

[plain]  view plain copy
  1. namespace java service.demo  
  2. service Hello {  
  3.     string helloString(1:string word)  
  4. }  

再生成JAVA文件与python文件

[plain]  view plain copy
  1. thrift --gen java demo.thrift  
  2. thrift --gen py demo.thrift  


接下来编译thrift的JAVA代码,解压 thrift-0.9.0.tar.gz ,在thrift-0.9.0\lib\java目录下用ant编译

编写JAVAServer

接口方法实现

[java]  view plain copy
  1. package service.demo;  
  2.   
  3. import org.apache.thrift.TException;  
  4.   
  5. import service.demo.Hello.Iface;  
  6.   
  7. public class HelloImpl implements Iface {  
  8.   
  9.     @Override  
  10.     public String helloString(String word) throws TException {  
  11.         System.out.println("get " + word);  
  12.         return "hello " + word;  
  13.     }  
  14.   
  15. }  
Server实现

[java]  view plain copy
  1. package service.demo;  
  2.   
  3. import org.apache.thrift.protocol.TBinaryProtocol;  
  4. import org.apache.thrift.protocol.TBinaryProtocol.Factory;  
  5. import org.apache.thrift.server.TServer;  
  6. import org.apache.thrift.server.TThreadPoolServer;  
  7. import org.apache.thrift.server.TThreadPoolServer.Args;  
  8. import org.apache.thrift.transport.TServerSocket;  
  9. import org.apache.thrift.transport.TTransportException;  
  10.   
  11. import service.demo.Hello.Processor;  
  12.   
  13. public class Server {  
  14.   
  15.     public void startServer() {  
  16.         try {  
  17.             TServerSocket serverTransport = new TServerSocket(1234);  
  18.             Hello.Processor process = new Processor(new HelloImpl());  
  19.             Factory portFactory = new TBinaryProtocol.Factory(truetrue);  
  20.             Args args = new Args(serverTransport);  
  21.             args.processor(process);  
  22.             args.protocolFactory(portFactory);  
  23.             TServer server = new TThreadPoolServer(args);  
  24.             server.serve();  
  25.         } catch (TTransportException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.       
  30.     public static void main(String[] args) {  
  31.         Server server = new Server();  
  32.         server.startServer();  
  33.     }  
  34. }  

JAVAClient实现

[java]  view plain copy
  1. package service.demo;  
  2.   
  3. import org.apache.thrift.TException;  
  4. import org.apache.thrift.protocol.TBinaryProtocol;  
  5. import org.apache.thrift.protocol.TProtocol;  
  6. import org.apache.thrift.transport.TSocket;  
  7. import org.apache.thrift.transport.TTransport;  
  8. import org.apache.thrift.transport.TTransportException;  
  9.   
  10. public class Client {  
  11.   
  12.     public void startClient() {  
  13.         TTransport transport;  
  14.         try {  
  15.             transport = new TSocket("localhost"1234);  
  16.             TProtocol protocol = new TBinaryProtocol(transport);  
  17.             Hello.Client client = new Hello.Client(protocol);  
  18.             transport.open();  
  19.             System.out.println(client.helloString("panguso"));  
  20.             transport.close();  
  21.         } catch (TTransportException e) {  
  22.             e.printStackTrace();  
  23.         } catch (TException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.   
  28.     public static void main(String[] args) {  
  29.         Client client = new Client();  
  30.         client.startClient();  
  31.     }  
  32. }  

编写pythonClient

首先要安装一下thrift的python支持,在thrift-0.9.0\lib\py下执行python setup.py install,此处要注意的是如果在eclise下编写代码要在pvdev->interpreter-python->system pythonpath下加入C:\Python27\Lib\site-packages\thrift-0.9.0-py2.7.egg

pythonclient实现

[python]  view plain copy
  1. from WebGetIce import Hello  
  2. from thrift.protocol import TBinaryProtocol  
  3. from thrift.transport import TSocket  
  4.   
  5. # Talk to a server via TCP sockets, using a binary protocol  
  6. transport = TSocket.TSocket("localhost"1234)  
  7. transport.open()  
  8. protocol = TBinaryProtocol.TBinaryProtocol(transport)  
  9.   
  10. # Use the service we already defined  
  11. client = Hello.Client(protocol)  
  12. print client.helloString("python")  
  13. # Retrieve something as well  

这篇关于thrift JAVA服务端 python客户端的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1127986

相关文章

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.