论坛首页 编程语言技术论坛

python 面向对象入门 - 之 Web Service

浏览 2620 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-03-02  
该博文涵盖了如下内容:
1. WSDL
2. SOAPpy
3.面向对象封装方法

#encoding=utf-8
"""
   Search Google from the command line
   url : http://pywebsvcs.sourceforge.net/
   Install  http://ncu.dl.sourceforge.net/project/pywebsvcs/SOAP.py/SOAPpy%200.11.0/SOAPpy-0.11.0.tar.gz
   author : edison & dive in python
"""

from SOAPpy import WSDL

#see http://code.google.com/intl/zh-CN/apis/soapsearch/api_faq.html
WSDLFILE = 'http://api.google.com/GoogleSearch.wsdl'
APIKEY = 'key'

#建立web service
_server = WSDL.Proxy(WSDLFILE)
#打印soap输入,输出
_server.soapproxy.config.dumpSOAPOut = 1 
_server.soapproxy.config.dumpSOAPIn = 1

def search(q):
    print "print server methods"
    print  _server.methods.keys()
    
    """Search Google and return list of {title, link, description}"""
    results = _server.doGoogleSearch(
        APIKEY, q, 0, 10, False, "", False, "", "utf-8", "utf-8")
    return [{"title": r.title.encode("utf-8"),
             "link": r.URL.encode("utf-8"),
             "description": r.snippet.encode("utf-8")}
            for r in results.resultElements]

if __name__ == '__main__':
    import sys
    if sys.argv[1] :
        for r in search(sys.argv[1])[:5]:
            print r['title']
            print r['link']
            print r['description']
            print

if __debug__:
    #只打印前5条
     for r in search("tl50")[:5]:
         print r['title']
         print r['link']
         print r['description']
         print
     pass

    
    
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics