Python 在suds中添加属性
我必须使用sud和Python执行soap请求Python 在suds中添加属性,python,web-services,suds,Python,Web Services,Suds,我必须使用sud和Python执行soap请求 <soap:Body> <registerOrder> <order merchantOrderNumber="" description="" amount="" currency="" language="" xmlns=""> <returnUrl>http://mysafety.com</returnUrl>
<soap:Body>
<registerOrder>
<order merchantOrderNumber="" description="" amount="" currency="" language="" xmlns="">
<returnUrl>http://mysafety.com</returnUrl>
</order>
</registerOrder>
</soap:Body>
http://mysafety.com
如何在registerOrder中添加属性?搜索MessagePlugin。您正在搜索的是编组选项。您需要将其作为插件添加到客户端:
self.client = Client(url, plugins=[MyPlugin()])
在封送方法中搜索context.envelope子项。python的vars()函数在这里非常有用。正如我所想,对你来说应该是这样的:
from suds.sax.attribute import Attribute
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
foo = context.envelope.getChild('Body').getChild('registerOrder')[0]
foo.attributes.append(Attribute("foo", "bar"))
上周我一直在做这件事,所以可能会为您节省一些时间:)更具动态性的MessagePlugin版本是:
from suds.sax.attribute import Attribute
from suds.plugin import MessagePlugin
class _AttributePlugin(MessagePlugin):
"""
Suds plug-in extending the method call with arbitrary attributes.
"""
def __init__(self, **kwargs):
self.kwargs = kwargs
def marshalled(self, context):
method = context.envelope.getChild('Body')[0]
for key, item in self.kwargs.iteritems():
method.attributes.append(Attribute(key, item))
用法:
client = Client(url)
# method 1
client.options.plugins = [_AttributePlugin(foo='bar')]
response = client.service.method1()
client.options.plugins = []
# method 2
response = client.service.method2()
您可以使用uuu inject Client选项来注入特定的xml
raw_xml = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
print client.service.example(__inject={'msg':raw_xml})
raw_xml=”“”
...
"""
print client.service.example(uuu inject={'msg':raw_xml})
另外,我更喜欢使用suds jurko,这是一种积极维护的sud分支。我已经重用并调整了代码以使用该想法。按以下方法更换肥皂水并使用
Client.<method>(param1=value1, ... , attributes={'attrName1':'attrVal1'} )
Client.(param1=value1,…,attributes={'attrName1':'attrVal1'})
根据需要使用“attrName1”属性调用“method”
--- a/website/suds/bindings/binding.py
+++ b/website/suds/bindings/binding.py
@@ -24,6 +24,7 @@ from suds.sax import Namespace
from suds.sax.parser import Parser
from suds.sax.document import Document
from suds.sax.element import Element
+from suds.sax.attribute import Attribute
from suds.sudsobject import Factory, Object
from suds.mx import Content
from suds.mx.literal import Literal as MxLiteral
@@ -101,7 +102,7 @@ class Binding:
"""
raise Exception, 'not implemented'
- def get_message(self, method, args, kwargs):
+ def get_message(self, method, args, kwargs, attributes=None):
"""
Get the soap message for the specified method, args and soapheaders.
This is the entry point for creating the outbound soap message.
@@ -115,11 +116,23 @@ class Binding:
@rtype: L{Document}
"""
+ if attributes:
+ pass
+ # moved to suds/bindings/document.py
+
+ #print method
+ #for name, val in attributes.items():
+ # method.attributes.append(Attribute(name, val))
+
+
content = self.headercontent(method)
header = self.header(content)
- content = self.bodycontent(method, args, kwargs)
+ content = self.bodycontent(method, args, kwargs, attributes=attributes)
body = self.body(content)
env = self.envelope(header, body)
+ #if attributes:
+ # print content
+ # 1/0
if self.options().prefixes:
body.normalizePrefixes()
env.promotePrefixes()
@@ -535,4 +548,4 @@ class PartElement(SchemaElement):
return self
else:
return self.__resolved
-
\ No newline at end of file
+
diff --git a/website/suds/bindings/document.py b/website/suds/bindings/document.py
index edd9422..0c84753 100644
--- a/website/suds/bindings/document.py
+++ b/website/suds/bindings/document.py
@@ -38,7 +38,7 @@ class Document(Binding):
(multiple message parts), must present a I{document} view for that method.
"""
- def bodycontent(self, method, args, kwargs):
+ def bodycontent(self, method, args, kwargs, attributes=None):
#
# The I{wrapped} vs I{bare} style is detected in 2 ways.
# If there is 2+ parts in the message then it is I{bare}.
@@ -54,6 +54,12 @@ class Document(Binding):
else:
root = []
n = 0
+
+ if attributes:
+ #print root.__class__
+ for name, val in attributes.items():
+ root.set(name, val)
+
for pd in self.param_defs(method):
if n < len(args):
value = args[n]
diff --git a/website/suds/client.py b/website/suds/client.py
index 8b4f258..f80e36a 100644
--- a/website/suds/client.py
+++ b/website/suds/client.py
@@ -592,7 +592,10 @@ class SoapClient:
timer.start()
result = None
binding = self.method.binding.input
- soapenv = binding.get_message(self.method, args, kwargs)
+ attributes = kwargs.get('attributes', None)
+ if attributes:
+ del kwargs['attributes']
+ soapenv = binding.get_message(self.method, args, kwargs, attributes)
timer.stop()
metrics.log.debug(
"message for '%s' created: %s",
@@ -841,4 +844,4 @@ class RequestContext:
@type error: A suds I{TransportError}.
"""
return self.client.failed(self.binding, error)
-
\ No newline at end of file
+
——a/website/suds/bindings/binding.py
+++b/website/suds/bindings/binding.py
@@-24,6+24,7@@from suds.sax导入命名空间
从suds.sax.parser导入解析器
从suds.sax.document导入文档
从suds.sax.element导入元素
+从suds.sax.attribute导入属性
从suds.sudsobject导入工厂中,对象
从suds.mx导入内容
从suds.mx.literal导入文本作为MxLiteral
@@-101,7+102,7@@class绑定:
"""
引发异常,“未实现”
-def get_消息(自身、方法、参数、kwargs):
+def get_消息(self、method、args、kwargs、attributes=None):
"""
获取指定方法、参数和SOAPHeader的soap消息。
这是创建出站soap消息的入口点。
@@-115,11+116,23@@class绑定:
@rtype:L{Document}
"""
+如果属性:
+通过
+#移动到suds/bindings/document.py
+
+#打印方法
+#对于名称,在attributes.items()中使用val:
+#method.attributes.append(Attribute(name,val))
+
+
内容=自身的主体内容(方法)
header=self.header(内容)
-content=self.bodycontent(方法、参数、kwargs)
+content=self.bodycontent(方法、args、kwargs、attributes=attributes)
body=self.body(内容)
env=自我信封(页眉、正文)
+#如果属性:
+#打印内容
+ # 1/0
如果self.options()前缀:
body.normalizePrefixes()
环境promotePrefixes()
@@-535,4+548,4@@class PartElement(SchemaElement):
回归自我
其他:
返回自我。已解决
-
\文件末尾没有换行符
+
diff--git a/website/suds/bindings/document.py b/website/suds/bindings/document.py
索引edd9422..0c84753 100644
---a/website/suds/bindings/document.py
+++b/website/suds/bindings/document.py
@@-38,7+38,7@@class文档(绑定):
(多个消息部分),必须为该方法提供一个I{document}视图。
"""
-def bodycontent(自身、方法、参数、kwargs):
+def bodycontent(self、method、args、kwargs、attributes=None):
#
#I{wrapped}vs I{bare}样式有两种检测方式。
#如果消息中有2+部分,那么它就是I{bare}。
@@-54,6+54,12@@class文档(绑定):
其他:
根=[]
n=0
+
+如果属性:
+#打印根目录。uu类__
+对于名称,在attributes.items()中使用val:
+root.set(名称,val)
+
对于自参数定义中的pd(方法):
如果n