gpt4 book ai didi

Python- Suds.Client.Service 问题,打印出标识符而不是方法文本

转载 作者:行者123 更新时间:2023-11-28 16:49:16 25 4
gpt4 key购买 nike

您好,我对 Python 和 Web 服务还很陌生,我已经开始尝试通过 Python 在 WSDL 中使用 Web 服务方法。现在,当我运行以下代码时,返回的是值“”。我的目标是获取 GetMessage 方法的返回值(一个简单的 Hello World),而是获取该方法的标识符。有没有办法得到这个返回值?

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)

但错误消息的其余部分将保留。有什么我想念的吗?我已经搜索了几个小时,但真的找不到太多显示类似问题的东西。

更新:

这是第一个代码段中打印出的内容:

<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')

塞巴斯蒂安想法的客户值(value)如下:

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

更新 2:每个 Sebastian 请求的日志记录

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

最佳答案

要修复 'Unsupported Media Type' 错误,您可以指定服务器支持的类型,例如:

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

要查看 suds 实际发送的内容,您可以配置日志记录:

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

关于Python- Suds.Client.Service 问题,打印出标识符而不是方法文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9769312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com