使用 SUDS 在 Python 中处理错误

发布于 2024-08-18 09:11:07 字数 2670 浏览 4 评论 0原文

我一直在尝试使用 SUDS 通过 wsdl 文件控制相机。我已经让代码正常工作,但我想将错误处理放入脚本中。我尝试了不同的例外,但无法使脚本正常工作。当我输入无效坐标时,出现错误。我正在使用的代码如下,后面是我收到的错误。

#!/home/build/Python-2.6.4/python

import suds
from suds.client import Client

####################################################################
#
#   Python SUDS Script that controls movement of Camera
#
####################################################################
#
#                    Absolute Move Function
#
####################################################################

def absoluteMove():

    # connects to WSDL file and stores location in variable 'client'
    client = Client('http://file.wsdl')

    # Create 'token' object to pass as an argument using the 'factory' namespace
    token = client.factory.create('ns4:ReferenceToken')
    print token

    # Create 'dest' object to pass as an argument and values passed to this object
    dest = client.factory.create('ns4:PTZVector')
    dest.PanTilt._x=400
    dest.PanTilt._y=0
    dest.Zoom._x=1
    print dest

    # Create 'speed' object to pass as an argument and values passed to this object
    speed = client.factory.create('ns4:PTZSpeed')
    speed.PanTilt._x=0
    speed.PanTilt._y=0
    speed.Zoom._x=1
    print speed

    # 'AbsoluteMove' method invoked passing in the new values entered in the above objects

    try:
        result = client.service.AbsoluteMove(token, dest, speed)
    except RuntimeError as detail:
        print 'Handling run-time error:', detail

    print "absoluteMove result ", result

result = absoluteMove() 

错误如下:

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
  File "ptztest.py", line 48, in <module>
    if __name__ == '__main__': result = absoluteMove()    
  File "ptztest.py", line 42, in absoluteMove
    result = client.service.AbsoluteMove(token, dest, speed)
  File "build/bdist.linux-i686/egg/suds/client.py", line 537, in __call__
  File "build/bdist.linux-i686/egg/suds/client.py", line 597, in invoke
  File "build/bdist.linux-i686/egg/suds/client.py", line 632, in send
  File "build/bdist.linux-i686/egg/suds/client.py", line 683, in failed
  File "build/bdist.linux-i686/egg/suds/bindings/binding.py", line 235, in get_fault
suds.WebFault: Server raised fault: 'Error setting requested pan'

我不确定我应该在这里使用哪个异常。有谁知道如何捕捉这个错误。值为 400 的 x 坐标以度为单位,这就是发生错误的原因。

谢谢

好的我已经找到解决方案了。在 SUDS 中,如果您在客户端定义中输入:

faults=False

,则会捕获故障并给出故障发生的原因。该行应为:

client = Client('http://file.wsdl', faults=False)

我标记为正确答案的帖子也能够发现问题已经发生。

谢谢大家

I have been trying to control a camera through a wsdl file using SUDS. I have got the code working but I want to place error handling into the script. I have tried different exceptions but am unable to get the script working. When I enter an invalid coordinate I get an error. The code I am using is below followed by the error I am recieving.

#!/home/build/Python-2.6.4/python

import suds
from suds.client import Client

####################################################################
#
#   Python SUDS Script that controls movement of Camera
#
####################################################################
#
#                    Absolute Move Function
#
####################################################################

def absoluteMove():

    # connects to WSDL file and stores location in variable 'client'
    client = Client('http://file.wsdl')

    # Create 'token' object to pass as an argument using the 'factory' namespace
    token = client.factory.create('ns4:ReferenceToken')
    print token

    # Create 'dest' object to pass as an argument and values passed to this object
    dest = client.factory.create('ns4:PTZVector')
    dest.PanTilt._x=400
    dest.PanTilt._y=0
    dest.Zoom._x=1
    print dest

    # Create 'speed' object to pass as an argument and values passed to this object
    speed = client.factory.create('ns4:PTZSpeed')
    speed.PanTilt._x=0
    speed.PanTilt._y=0
    speed.Zoom._x=1
    print speed

    # 'AbsoluteMove' method invoked passing in the new values entered in the above objects

    try:
        result = client.service.AbsoluteMove(token, dest, speed)
    except RuntimeError as detail:
        print 'Handling run-time error:', detail

    print "absoluteMove result ", result

result = absoluteMove() 

The error is below:

No handlers could be found for logger "suds.client"
Traceback (most recent call last):
  File "ptztest.py", line 48, in <module>
    if __name__ == '__main__': result = absoluteMove()    
  File "ptztest.py", line 42, in absoluteMove
    result = client.service.AbsoluteMove(token, dest, speed)
  File "build/bdist.linux-i686/egg/suds/client.py", line 537, in __call__
  File "build/bdist.linux-i686/egg/suds/client.py", line 597, in invoke
  File "build/bdist.linux-i686/egg/suds/client.py", line 632, in send
  File "build/bdist.linux-i686/egg/suds/client.py", line 683, in failed
  File "build/bdist.linux-i686/egg/suds/bindings/binding.py", line 235, in get_fault
suds.WebFault: Server raised fault: 'Error setting requested pan'

I am not sure which exception I should be using here. Does anyone know how to catch this error. The x coordinate with the value 400 is in degree's that is why the error happens.

Thanks

Okay I have found the solution. In SUDS if you enter:

faults=False

into the client definition, this catches faults and gives the reason why the fault happened. The line should read:

client = Client('http://file.wsdl', faults=False)

The post that I have marked as the correct answer also is able to catch that a problem has happened.

Thanks all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葮薆情 2024-08-25 09:11:07

如果您处理了代码中的所有异常和错误,并且代码工作正常,但您仍然收到以下带有正确输出的消息。

Msg : "No handlers can be found for logger suds.client "

添加这一行。

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

那么一个简单的解决方案是在 yourclient.py 文件中的所有 import 语句之后

If you handled all exceptions and errors in your code and your code is working fine but still you are getting below message with your correct output.

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

Then a simple solution is to add this line

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

in yourclient.py file just after all import statement.

风铃鹿 2024-08-25 09:11:07

如果你想捕获该异常,你应该把

try:
    result = client.service.AbsoluteMove(token, dest, speed)
except suds.WebFault as detail:
    ...

If you want to catch that exception you should put

try:
    result = client.service.AbsoluteMove(token, dest, speed)
except suds.WebFault as detail:
    ...
温柔嚣张 2024-08-25 09:11:07

You need to catch suds.WebFault by the looks of that traceback. The error itself seems legitimate, IE, your requests are being executed correctly, but perhaps your parameters are wrong in the given context.

棒棒糖 2024-08-25 09:11:07

我相信您在评论中提到了无害的诊断消息。我可以通过将 logging.INFO 分配给 basicConfiglogging.CRITICAL 来抑制来自调用 logging.error() 的 sud 的消息> 到 suds.client

https://fedorahosted.org/suds/wiki/Documentation

I believe you refer to a harmless diagnostic message in your comment. I could suppress messages from suds calling logging.error() by assigning logging.INFO to basicConfig and logging.CRITICAL to suds.client.

https://fedorahosted.org/suds/wiki/Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文