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

来源:互联网 发布:star法则简历模板java 编辑:程序博客网 时间:2024/04/30 21:56

最近用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  
0 0
原创粉丝点击