本文整理匯總了Python中soaplib.xml.ElementTree.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python ElementTree.parse方法的具體用法?Python ElementTree.parse怎麽用?Python ElementTree.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類soaplib.xml.ElementTree
的用法示例。
在下文中一共展示了ElementTree.parse方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: join_attachment
# 需要導入模塊: from soaplib.xml import ElementTree [as 別名]
# 或者: from soaplib.xml.ElementTree import parse [as 別名]
def join_attachment(id, envelope, payload, prefix=True):
'''
Helper function for swa_to_soap.
Places the data from an attachment back into a SOAP message, replacing
its xop:Include element or href.
@param id content-id or content-location of attachment
@param prefix Set this to true if id is content-id or false if
it is content-location. It prefixes a "cid:" to
the href value.
@param envelope soap envelope string to be operated on
@param payload attachment data
@return tuple of length 2 with the new message and the
number of replacements made
'''
# grab the XML element of the message in the SOAP body
soapmsg = StringIO(envelope)
soaptree = ElementTree.parse(soapmsg)
soapns = soaptree.getroot().tag.split('}')[0].strip('{')
soapbody = soaptree.getroot().find("{%s}Body" % soapns)
message = None
for child in list(soapbody):
if child.tag != "%sFault" % (soapns, ):
message = child
break
numreplaces = 0
idprefix = ''
if prefix == True:
idprefix = "cid:"
id = "%s%s" % (idprefix, id, )
# Make replacement.
for param in message:
# Look for Include subelement.
for sub in param:
if sub.tag.split('}')[-1] == 'Include' and \
sub.attrib.get('href') == id:
param.remove(sub)
param.text = payload
numreplaces += 1
if numreplaces < 1 and param.attrib.get('href') == id:
del(param.attrib['href'])
param.text = payload
numreplaces += 1
soapmsg.close()
soapmsg = StringIO()
soaptree.write(soapmsg)
joinedmsg = soapmsg.getvalue()
soapmsg.close()
return (joinedmsg, numreplaces)
示例2: test_join_attachment
# 需要導入模塊: from soaplib.xml import ElementTree [as 別名]
# 或者: from soaplib.xml.ElementTree import parse [as 別名]
def test_join_attachment(self):
id="http://tempuri.org/1/634133419330914808"
payload="ANJNSLJNDYBC SFDJNIREMX:CMKSAJN"
envelope = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><DownloadPartFileResponse xmlns="http://tempuri.org/"><DownloadPartFileResult xmlns:a="http://schemas.datacontract.org/2004/07/KlanApi.Common" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ErrorCode>0</a:ErrorCode><a:ErrorMessage i:nil="true"/><a:Data><xop:Include href="cid:http%3A%2F%2Ftempuri.org%2F1%2F634133419330914808" xmlns:xop="http://www.w3.org/2004/08/xop/include"/></a:Data></DownloadPartFileResult></DownloadPartFileResponse></s:Body></s:Envelope>'
(joinedmsg, numreplaces) = join_attachment(id, envelope, payload)
soapmsg = StringIO(joinedmsg)
soaptree = ElementTree.parse(soapmsg)
soapns = "http://schemas.xmlsoap.org/soap/envelope/"
r = DownloadPartFileResult.from_xml( soaptree.getroot().find("{%s}Body" % soapns).getchildren()[0].getchildren()[0] )
self.assertEquals(payload, r.Data)
示例3: apply_mtom
# 需要導入模塊: from soaplib.xml import ElementTree [as 別名]
# 或者: from soaplib.xml.ElementTree import parse [as 別名]
def apply_mtom(headers, envelope, params, paramvals):
'''
Apply MTOM to a SOAP envelope, separating attachments into a
MIME multipart message.
References:
XOP http://www.w3.org/TR/xop10/
MTOM http://www.w3.org/TR/soap12-mtom/
http://www.w3.org/Submission/soap11mtom10/
@param headers Headers dictionary of the SOAP message that would
originally be sent.
@param envelope SOAP envelope string that would have originally been sent.
@param params params attribute from the Message object used for the SOAP
@param paramvals values of the params, passed to Message.to_xml
@return tuple of length 2 with dictionary of headers and
string of body that can be sent with HTTPConnection
'''
# grab the XML element of the message in the SOAP body
soapmsg = StringIO(envelope)
soaptree = ElementTree.parse(soapmsg)
soapns = soaptree.getroot().tag.split('}')[0].strip('{')
soapbody = soaptree.getroot().find("{%s}Body" % soapns)
message = None
for child in list(soapbody):
if child.tag != "%sFault" % (soapns, ):
message = child
break
# Get additional parameters from original Content-Type
ctarray = []
for n, v in headers.items():
if n.lower() == 'content-type':
ctarray = v.split(';')
break
roottype = ctarray[0].strip()
rootparams = {}
for ctparam in ctarray[1:]:
n, v = ctparam.strip().split('=')
rootparams[n] = v.strip("\"'")
# Set up initial MIME parts
mtompkg = MIMEMultipart('related',
boundary='?//<><>soaplib_MIME_boundary<>')
rootpkg = None
try:
rootpkg = MIMEApplication(envelope, 'xop+xml', encode_7or8bit)
except NameError:
rootpkg = MIMENonMultipart("application", "xop+xml")
rootpkg.set_payload(envelope)
encode_7or8bit(rootpkg)
# Set up multipart headers.
del(mtompkg['mime-version'])
mtompkg.set_param('start-info', roottype)
mtompkg.set_param('start', '<soaplibEnvelope>')
if 'SOAPAction' in headers:
mtompkg.add_header('SOAPAction', headers.get('SOAPAction'))
# Set up root SOAP part headers.
del(rootpkg['mime-version'])
rootpkg.add_header('Content-ID', '<soaplibEnvelope>')
for n, v in rootparams.items():
rootpkg.set_param(n, v)
rootpkg.set_param('type', roottype)
mtompkg.attach(rootpkg)
# Extract attachments from SOAP envelope.
for i in range(len(params)):
name, typ = params[i]
if typ == Attachment:
id = "soaplibAttachment_%s" % (len(mtompkg.get_payload()), )
param = message[i]
param.text = ""
incl = create_xml_subelement(param,
"{http://www.w3.org/2004/08/xop/include}Include")
incl.attrib["href"] = "cid:%s" % id
if paramvals[i].fileName and not paramvals[i].data:
paramvals[i].load_from_file()
data = paramvals[i].data
attachment = None
try:
attachment = MIMEApplication(data, _encoder=encode_7or8bit)
except NameError:
attachment = MIMENonMultipart("application", "octet-stream")
attachment.set_payload(data)
encode_7or8bit(attachment)
del(attachment['mime-version'])
attachment.add_header('Content-ID', '<%s>' % (id, ))
mtompkg.attach(attachment)
# Update SOAP envelope.
soapmsg.close()
soapmsg = StringIO()
soaptree.write(soapmsg)
rootpkg.set_payload(soapmsg.getvalue())
soapmsg.close()
#.........這裏部分代碼省略.........