如何用Python向SOAP服务器发送多部分/相关请求?

2024-03-29 12:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须通过多部分/相关的httppost将文件发送到SOAP服务器。在

我从零开始构建了这样的信息:

from email.mime.application import MIMEApplication
from email.encoders import encode_7or8bit
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase

envelope = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xmime5="http://www.w3.org/2005/05/xmlmime" xmlns:osc-data="http://oracc.org/wsdl/ows.xsd" xmlns:osc-meth="http://oracc.org/wsdl/ows.wsdl"><SOAP-ENV:Body><osc-meth:Request><osc-data:keys><osc-data:key>atf</osc-data:key><osc-data:key>tests/mini</osc-data:key><osc-data:key>00atf/hyphens.atf</osc-data:key></osc-data:keys><osc-data:data><osc-data:item xmime5:contentType="*/*"><xop:Include href="cid:id6"/></osc-data:item></osc-data:data></osc-meth:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>"""

mtompkg = MIMEMultipart('related',boundary='============boundary============', charset='utf-8', type='application/xop+xml', start='<SOAP-ENV:Envelope>')
#Doesn't like the hyphen in start-info when passing as MIMEMultipart param
mtompkg.set_param('start-info', 'application/soap+xml')
mtompkg['Host'] = "http://oracc.museum.upenn.edu:8085"
mtompkg['Connection'] = "close"
del(mtompkg['mime-version'])

rootpkg = MIMEApplication(envelope, 'xop+xml', encode_7or8bit)
rootpkg.set_param('charset', 'utf-8')
rootpkg.set_param('type', 'application/soap+xml')
rootpkg.add_header('Content-ID', '<SOAP-ENV:Envelope>')
del(rootpkg['Content-Transfer-Encoding'])
rootpkg.add_header('Content-Transfer-Encoding', 'binary')
del(rootpkg['mime-version'])

mtompkg.attach(rootpkg)

document = MIMEBase('*','*')
document['Content-Transfer-Encoding'] = "binary"
document['Content-ID'] = "<id6>"
filename = "./request.zip"
document.set_payload(open(filename,'rb').read())
del(document['mime-version'])

mtompkg.attach(document)

bound = '--%s' % (mtompkg.get_boundary(), )
marray = mtompkg.as_string().split(bound)
mtombody = bound
mtombody += bound.join(marray[1:])

mtompkg.add_header("Content-Length", str(len(mtombody)))

结果得到了一条与我从SOAP服务器开发人员那里得到的示例相匹配的消息:

^{pr2}$

为了发送它,我尝试了请求,urllib2和httplib,我得到的行为是服务器永远挂起。我试着设置一个超时,希望它能给我一些服务器输出,但它不起作用。我试过了:

根据请求:

import requests
url = 'http://MY_URL:SOME_PORT'
headers = dict(mtompkg.items())
body = mtompkg.as_string().split('\n\n', 1)[1]
response = requests.post(url, data=body, headers=headers)

使用urllib2:

import urllib2
request = urllib2.Request('http://MY_URL:SOME_PORT', body, headers)
urllib2.urlopen(request).read()

使用httplib:

import urlparse
import httplib
schema, netloc, url, params, query, fragments = urlparse.urlparse('http://MY_URL:SOME_PORT')
http = httplib.HTTPConnection(netloc)
http.connect()
http.putrequest("POST", 'http://MY_URL:SOME_PORT')
http.putheader('Content-type',  mtompkg['Host'])
http.putheader('Content-type',mtompkg['Content-Type'])
http.putheader('Content-Length", str(1429))
http.endheaders()
http.send(body)
response = http.getresponse()

更高级的选项,如suds、suds jurko、SOAPpy等,似乎不支持多种形式/相关的附件。我已经没有办法测试了,所以任何形式的帮助都将是非常感谢的。在


Tags: keyorgimportenvhttpdatawwwcontent