Python 使用urllib2执行SOAP POST,但我一直收到一个错误

Python 使用urllib2执行SOAP POST,但我一直收到一个错误,python,api,soap,post,typeerror,Python,Api,Soap,Post,Typeerror,我正试图通过一个SOAP帖子进行API调用,我不断地得到消息 “TypeError:不是有效的非字符串序列或映射对象。”@data=urllib.urlencode(值) SM_模板=”“” 合作伙伴id """ 值=SM_模板%() data=urllib.urlencode(值) 请求(站点、数据) 响应=urllib2.urlopen(请求) 页面=response.read() 任何帮助都将不胜感激。函数urllib.urlencode需要一系列键值对或类似dict的映射类型: >

我正试图通过一个SOAP帖子进行API调用,我不断地得到消息 “TypeError:不是有效的非字符串序列或映射对象。”@data=urllib.urlencode(值)

SM_模板=”“”
合作伙伴id
"""
值=SM_模板%()
data=urllib.urlencode(值)
请求(站点、数据)
响应=urllib2.urlopen(请求)
页面=response.read()

任何帮助都将不胜感激。

函数
urllib.urlencode
需要一系列键值对或类似
dict
的映射类型:

>>> urllib.urlencode([('a','1'), ('b','2'), ('b', '3')])
'a=1&b=2&b=3'
要执行soaphttppost,应该保持SM_模板blob不变,并将其设置为POST主体,然后为POST主体的编码和字符集添加内容类型头。例如:

data = SM_TEMPLATE
headers = {
    'Content-Type': 'application/soap+xml; charset=utf-8'
    }
req = urllib2.Request(site, data, headers)

以下面的代码为例,它可以帮助您使用适用于Python 2.6.6的urllib2解决SOAP请求。我打电话给Oracle Data Integrator(Oracle ODI)时,它起到了作用。显然,您必须调整适合您的情况的值:

import urllib2

url = "http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl"

post_data = """<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        what_you_want_to_send_in_a_correct_format
    </Body>
</Envelope>
"""

http_headers = {
    "Accept": "application/soap+xml,multipart/related,text/*",
    "Cache-Control": "no-cache",
    "Pragma": "no-cache",
    "Content-Type": "text/xml; charset=utf-8"

}

request_object = urllib2.Request(url, post_data, http_headers)

#DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES
http_proxy_server = "10.1.2.3"
http_proxy_port = "8080"
http_proxy_realm = http_proxy_server
http_proxy_full_auth_string = "http://%s:%s" % (http_proxy_server, http_proxy_port)
proxy = urllib2.ProxyHandler({'http': http_proxy_full_auth_string})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
#END OF --> DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES

response = urllib2.urlopen(request_object)
html_string = response.read()
print html_string
导入urllib2
url=”http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl"
post_data=“”
您希望以正确的格式发送什么
"""
http_头={
“接受”:“应用程序/soap+xml,多部分/相关,文本/*”,
“缓存控制”:“无缓存”,
“Pragma”:“无缓存”,
“内容类型”:“text/xml;charset=utf-8”
}
request\u object=urllib2.request(url、post\u数据、http\u头)
#如果不使用代理,请删除此块
http\u proxy\u server=“10.1.2.3”
http_proxy_port=“8080”
http\u proxy\u realm=http\u proxy\u服务器
http\u proxy\u full\u auth\u string=“http://%s:%s”%(http\u proxy\u服务器,http\u proxy\u端口)
proxy=urllib2.ProxyHandler({'http':http\u proxy\u full\u auth\u string})
opener=urlib2.build\u opener(代理)
urllib2.install_opener(opener)
#结束-->如果不使用代理,请删除此块
response=urlib2.urlopen(请求\对象)
html_string=response.read()
打印html\u字符串

任何反馈都将不胜感激:-)

我还需要添加“soapAction”:“GetAllItem”以满足我的工作请求
import urllib2

url = "http://alexmoleiro.com:20910/oraclediagent/OdiInvoke?wsdl"

post_data = """<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        what_you_want_to_send_in_a_correct_format
    </Body>
</Envelope>
"""

http_headers = {
    "Accept": "application/soap+xml,multipart/related,text/*",
    "Cache-Control": "no-cache",
    "Pragma": "no-cache",
    "Content-Type": "text/xml; charset=utf-8"

}

request_object = urllib2.Request(url, post_data, http_headers)

#DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES
http_proxy_server = "10.1.2.3"
http_proxy_port = "8080"
http_proxy_realm = http_proxy_server
http_proxy_full_auth_string = "http://%s:%s" % (http_proxy_server, http_proxy_port)
proxy = urllib2.ProxyHandler({'http': http_proxy_full_auth_string})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
#END OF --> DELETE THIS BLOCK IF YOU ARE NOT USING PROXIES

response = urllib2.urlopen(request_object)
html_string = response.read()
print html_string