python 面向对象入门 - 之 Web Service

1 篇文章 0 订阅
1 篇文章 0 订阅
该博文涵盖了如下内容: 
1. WSDL 
2. SOAPpy 
3.面向对象封装方法 

Java代码   收藏代码
  1. #encoding=utf-8  
  2. """  
  3.    Search Google from the command line  
  4.    url : http://pywebsvcs.sourceforge.net/  
  5.    Install  http://ncu.dl.sourceforge.net/project/pywebsvcs/SOAP.py/SOAPpy%200.11.0/SOAPpy-0.11.0.tar.gz  
  6.    author : edison & dive in python  
  7. """  
  8.   
  9. from SOAPpy import WSDL  
  10.   
  11. #see http://code.google.com/intl/zh-CN/apis/soapsearch/api_faq.html  
  12. WSDLFILE = 'http://api.google.com/GoogleSearch.wsdl'  
  13. APIKEY = 'key'  
  14.   
  15. #建立web service  
  16. _server = WSDL.Proxy(WSDLFILE)  
  17. #打印soap输入,输出  
  18. _server.soapproxy.config.dumpSOAPOut = 1   
  19. _server.soapproxy.config.dumpSOAPIn = 1  
  20.   
  21. def search(q):  
  22.     print "print server methods"  
  23.     print  _server.methods.keys()  
  24.       
  25.     """Search Google and return list of {title, link, description}"""  
  26.     results = _server.doGoogleSearch(  
  27.         APIKEY, q, 010, False, "", False, """utf-8""utf-8")  
  28.     return [{"title": r.title.encode("utf-8"),  
  29.              "link": r.URL.encode("utf-8"),  
  30.              "description": r.snippet.encode("utf-8")}  
  31.             for r in results.resultElements]  
  32.   
  33. if __name__ == '__main__':  
  34.     import sys  
  35.     if sys.argv[1] :  
  36.         for r in search(sys.argv[1])[:5]:  
  37.             print r['title']  
  38.             print r['link']  
  39.             print r['description']  
  40.             print  
  41.   
  42. if __debug__:  
  43.     #只打印前5条  
  44.      for r in search("tl50")[:5]:  
  45.          print r['title']  
  46.          print r['link']  
  47.          print r['description']  
  48.          print  
  49.      pass  
  50.   
  51.       
  52.       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值