使用urllib2进行SOAP POST,但我一直收到错误

Geo*_*rge 4 python api post soap typeerror

我正在尝试通过SOAP POST进行API调用,并且我不断收到"TypeError:不是有效的非字符串序列或映射对象".@ data = urllib.urlencode(values)

SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <AutotaskIntegrations xmlns="http://Autotask.net/ATWS/v1_5/">
      <PartnerID>partner id</PartnerID>
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <getThresholdAndUsageInfo xmlns="http://Autotask.net/ATWS/v1_5/">
    </getThresholdAndUsageInfo>
  </soap:Body>
</soap:Envelope>"""

values = SM_TEMPLATE%()
data = urllib.urlencode(values)
req = urllib2.Request(site, data)
response = urllib2.urlopen(req)
the_page = response.read()
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

sam*_*ias 5

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

>>> urllib.urlencode([('a','1'), ('b','2'), ('b', '3')])
'a=1&b=2&b=3'
Run Code Online (Sandbox Code Playgroud)

要执行SOAP HTTP POST,您应该保留SM_TEMPLATE blob,并将其设置为POST主体,然后为POST主体的编码和字符集添加Content-Type标头.例如:

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