2011-03-01 32 views
0

我有以下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

回答

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() 
+1

如果可能的話,請還包括一些解釋。 – 2014-10-20 01:48:47