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

问题描述:

我有以下Python 2.6,它完美的作品。Python发布到肥皂服务未定义的行为

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) 

我在做什么错了?

+0

很显然,如果我评论了符合主机头,一切工作正常的3.1版本。 – PCManticore 2011-03-01 20:32:29

+0

为什么不为内容类型(''text/xml; charset =“UTF-8”'')使用单引号而不是转义双引号?阅读和调试会更容易。 – Velociraptors 2011-03-01 20:40:26

+0

你可以打开调试,看看它说什么?我没有Python 3,但在2.x中,您将设置'HTTPConnection.debuglevel = 1'。如果您不指定它,库会添加主机本身,并且调试会告诉您它添加的内容。 – SteveMc 2011-03-01 21:10:14

这是我的解决方案(关于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() 
+1

如果可能的话,请还包括一些解释。 – 2014-10-20 01:48:47