2012-03-19 26 views
3

您好,我對Python和Web服務相當陌生,我已經開始嘗試通過Python在WSDL中使用Web服務方法。現在,當我運行下面的代碼時,返回的是值「」。我的目標是獲取GetMessage方法的返回值(一個簡單的Hello World),而是獲取該方法的標識符。有沒有辦法獲得這個返回值?Python- Suds.Client.Service問題,打印出標識符而不是方法文本

import logging 
import sys 
from suds.client import Client 
sys.setrecursionlimit(2000) 
url='http://localhost:50774/HostDevServer/HelloWorldService.svc?wsdl' 
client = Client(url) 
example = client.service.GetMessage 
print example 

我也曾嘗試下面的代碼

import logging 
import sys 
from suds.client import Client 
sys.setrecursionlimit(2000) 
url='http://localhost:50774/HostDevServer/HelloWorldService.svc?wsdl' 
client = Client(url) 
example = client.service.GetMessage() 
print example 

,這將返回以下錯誤消息:

No handlers could be found for logger "suds.client" 

Traceback (most recent call last): 
    File "C:\Python27\exampleForSuds.py", line 7, in <module> 
    example = client.service.GetMessage() 
    File "build\bdist.win32\egg\suds\client.py", line 542, in __call__ 
    return client.invoke(args, kwargs) 
    File "build\bdist.win32\egg\suds\client.py", line 602, in invoke 
    result = self.send(soapenv) 
    File "build\bdist.win32\egg\suds\client.py", line 649, in send 
    result = self.failed(binding, e) 
    File "build\bdist.win32\egg\suds\client.py", line 708, in failed 
    raise Exception((status, reason)) 
Exception: (415, u'Unsupported Media Type') 

我能找到解決的第一個錯誤,加入沒有指出處理程序:

logging.getLogger('suds.client').setLevel(logging.CRITICAL) 

但是,其餘的錯誤信息將保留。有什麼我錯過了嗎?我現在已經搜索了幾個小時,並且找不到任何顯示類似問題的東西。

UPDATE:

這就是第一個代碼段打印出來:

<suds.client.Method instance at 0x02D9FC38> 

我試圖塞巴斯蒂安想法,我所得到的是這樣的:

Traceback (most recent call last): 
    File "C:\Python27\exampleForSuds.py", line 10, in <module> 
    example = client.service.GetMessage() 
    File "build\bdist.win32\egg\suds\client.py", line 542, in __call__ 
    return client.invoke(args, kwargs) 
    File "build\bdist.win32\egg\suds\client.py", line 602, in invoke 
    result = self.send(soapenv) 
    File "build\bdist.win32\egg\suds\client.py", line 649, in send 
    result = self.failed(binding, e) 
    File "build\bdist.win32\egg\suds\client.py", line 708, in failed 
    raise Exception((status, reason)) 
Exception: (400, u'Bad Request') 

的塞巴斯蒂安想法的客戶價值如下:

Suds (https://fedorahosted.org/suds/) version: 0.4 GA build: R699-20100913 

Service (HelloWorldService) tns="http://tempuri.org/" 
    Prefixes (1) 
     ns0 = "http://schemas.microsoft.com/2003/10/Serialization/" 
    Ports (1): 
     (WSHttpBinding_IHelloWorldService) 
     Methods (1): 
      GetMessage() 
     Types (3): 
      ns0:char 
      ns0:duration 
      ns0:guid 

UPDATE2:每塞巴斯蒂安請求測井

DEBUG:suds.transport.http:sending: 
URL:http://localhost:50774/HostDevServer/HelloWorldService.svc 
HEADERS: {'SOAPAction': u'"http://tempuri.org/IHelloWorldService/GetMessage"', 'Content-Type': 'application/soap+xml; charset="UTF-8"', 'Content-type': 'application/soap+xml; charset="UTF-8"', 'Soapaction': u'"http://tempuri.org/IHelloWorldService/GetMessage"'} 
MESSAGE: 
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><ns1:Body><ns0:GetMessage/></ns1:Body></SOAP-ENV:Envelope> 
ERROR:suds.client:<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:ns0="http://tempuri.org/" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Header/> 
    <ns1:Body> 
     <ns0:GetMessage/> 
    </ns1:Body> 
</SOAP-ENV:Envelope> 

Traceback (most recent call last): 
    File "C:\Python27\exampleForSuds.py", line 11, in <module> 
    example = client.service.GetMessage() 
    File "build\bdist.win32\egg\suds\client.py", line 542, in __call__ 
    return client.invoke(args, kwargs) 
    File "build\bdist.win32\egg\suds\client.py", line 602, in invoke 
    result = self.send(soapenv) 
    File "build\bdist.win32\egg\suds\client.py", line 649, in send 
    result = self.failed(binding, e) 
    File "build\bdist.win32\egg\suds\client.py", line 708, in failed 
    raise Exception((status, reason)) 
Exception: (400, u'Bad Request') 

的代碼,以及用於這樣的錯誤:

import logging 
import sys 
logging.basicConfig(level=logging.INFO) 
logging.getLogger('suds.transport.http').setLevel(logging.DEBUG) 
from suds.client import Client 
sys.setrecursionlimit(2000) 
url='http://localhost:50774/HostDevServer/HelloWorldService.svc?wsdl' 
headers = {'Content-Type': 'application/soap+xml; charset="UTF-8"'} 
client = Client(url,headers=headers) 
print client 
example = client.service.GetMessage() 
print example 

最後更新:

找到了解決該問題,原來的問題與python代碼無關,而與web服務的web.config無關。它需要使用basicHttpBinding的

+0

你能否更新顯示'print client'輸出的問題? – dusan 2012-03-19 12:22:53

+0

在那裏更新它 – 2012-03-19 12:55:07

+1

'client.service.GetMessage()'似乎是正確的。啓用日誌記錄並顯示導致「400,u'Bad Request」錯誤及其輸出的實際代碼。 – jfs 2012-03-19 13:39:04

回答

3

要解決'Unsupported Media Type'錯誤,您可以指定服務器支持如,:

headers = {'Content-Type': 'application/soap+xml; charset="UTF-8"'} 
client = Client(wsdl_url, headers=headers) 

要看到什麼肥皂水實際發送您可以配置日誌記錄類型:

import logging 
logging.basicConfig(level=logging.INFO) 
logging.getLogger('suds.transport.http').setLevel(logging.DEBUG) 
+0

我已經嘗試過他,但是這樣做我得到以下內容: 400,u'Bad請求' – 2012-03-19 12:37:28

+1

@AndrewFerguson:顯示'print client'以查看'GetMessage()'期望的參數 – jfs 2012-03-19 12:52:50

+0

用打印值 – 2012-03-19 12:58:48

相關問題