Python发布到soap服务未定义的行为

时间:2011-03-01 20:25:04

标签: python soap

我在Python 2.6中有以下内容,它可以很好地工作。

webservice = httplib.HTTP("www.racai.ro:80")
webservice.putrequest("POST", "/webservices/TextProcessing.asmx?WSDL")  
webservice.putheader("Host", "www.racai.ro")
webservice.putheader("User-Agent", "Python")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(f))
webservice.endheaders()
webservice.send(f)

现在,我在Python 3.1中有以下内容,我收到错误请求(无效的标题名称)。

tstring = template.format(text)   
webservice = http.client.HTTPConnection("www.racai.ro:80")
webservice.putrequest("POST", "/webservices/TextProcessing.asmx?WSDL")
webservice.putheader("Host", "www.racai.ro")
webservice.putheader("User-Agent", "Python")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(tstring))
webservice.endheaders()
tstring = tstring.encode()
webservice.send(tstring)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是我的解决方案(在Python 3.3上):

def send_soap_request(soap_message):
   webservice = HTTPConnection('www.example.host:80')
   request_headers = {"Host": 'www.example.host:80',
                   "Content-type": 'text/xml;charset="UTF-8"',
                   "SOAPAction": '""', }
   webservice.request("POST", '/messager/example_service/sendMessage', soap_message.encode('utf8'), request_headers)
   webservice.getresponse()
   webservice.close()