博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Python实现文件上传下载的SOAP Client

Posted on 2009-08-03 22:10  莫森  阅读(1494)  评论(0)    收藏  举报
suds是Python的一个soap库。下载suds。把文件拷入python的库目录下。

soapclient.py
from suds.client import Client
from suds.sax.element import Element
import base64

def upFile(client,file):
    f = open(file,'rb')
    fs = f.read()

    attach = client.factory.create('ns0:Base64Attachment') # 'ns0:Base64Attachment'为wsdl中定义的类型
    attach.base64FileStr = base64.encodestring(fs)
    attach.attName='test'
    r = client.service.upFile(attach)

def downFile(client,fileID):
    r = client.service.downFile(fileID)

if __name__=="__main__":
    url='http://localhost/services/FileUpAndDownService?wsdl'
    client = Client(url)

    # set soap header
    u = Element('Username')
    u.setText('user')
    p = Element('Password')
    p.setText('password')
    client.set_options(soapheaders=(u,p))  

    upFile(client,r"c:\test")