Python suds错误服务器引发的错误:'Session expired'

时间:2015-07-07 04:16:22

标签: php python soap suds

我正在尝试编写一个python脚本来调用SOAP PHP API方法。我已经得到它去成功获得所需的令牌,但当我实际上尝试调用任何方法时,我得到一个错误。

以下是我正在使用的代码,其中某些数据已经过编辑

from suds.client import Client
from suds.xsd.doctor import ImportDoctor, Import
import logging
from suds import WebFault
import suds


username = "apiUserAccount"
password = "apiPassword"
keyword = "computerNameImSearchingFor"

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

url = 'url/to/service.php?wsdl'  # wsdl api url
tns = 'url/to/api/'  # target name space from php
#fixing broken schema
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp.filter.add(tns)

client = Client(url, plugins=[ImportDoctor(imp)])  # calling api
print client

#This calls the request for an authentication token
auth_token = client.service.auth_request_api(username, password)
username = "myUserName"
password = "myPassword"
user_login = client.service.auth_request_user(auth_token, username, password)
#this works
print "api token: " + api_token
#this also works
print "login token: " + login

#This doesn't work...
try:
    print client.service.search_in_namehost(user_login, keyword)
except suds.WebFault as detail:
    print detail

错误在search_in_namehost方法调用中抛出,这是输出,我为了空间的缘故删除了一些

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

Service ( SERVICENAME ) tns="address/to/server/api/"
   Prefixes (2)
      ns0 = "http://schemas.xmlsoap.org/soap/encoding/"
      ns1 = "http://address/to/server/api/"
   Ports (1):
      (SERVICENAME_serviceSoap)
         Methods (20):
            auth_request_api(xs:string username, xs:string password, )
            auth_request_user(xs:string auth_token, xs:string username, xs:string password, )
            search_in_namehost(xs:string login_token, xs:string keyword, )
         Types (50):
            ns0:Array
            ns0:ENTITIES
            ns0:ENTITY
            ns0:ID
            ns0:IDREF
            ns0:IDREFS

api token: alphaNumericApiToken
login token: alphaNumericApiToken
Traceback (most recent call last):
  File "/path/to/my/python/script.py", line 42, in <module>
    result = client.service.incidents_list(user_login)
  File "build/bdist.macosx-10.10-intel/egg/suds/client.py", line 542, in __call__
  File "build/bdist.macosx-10.10-intel/egg/suds/client.py", line 602, in invoke
  File "build/bdist.macosx-10.10-intel/egg/suds/client.py", line 643, in send
  File "build/bdist.macosx-10.10-intel/egg/suds/client.py", line 678, in succeeded
  File "build/bdist.macosx-10.10-intel/egg/suds/bindings/binding.py", line 154, in get_reply
  File "build/bdist.macosx-10.10-intel/egg/suds/bindings/binding.py", line 498, in returned_types
  File "build/bdist.macosx-10.10-intel/egg/suds/bindings/binding.py", line 441, in bodypart_types
suds.TypeNotFound: Type not found: '(array, http://internalserver/servicename/api/, )'

Process finished with exit code 1

当我创建一个test.php文件来使用文档调用方法时,我确实得到了我想要的数据。这是php代码:

<?php
include("soapclient.php");

$tech_username = "api_user";
$tech_password = "apipassword";
$username = "user";
$password = "password";
$keyword = "computername";
$client = new SERVICE_serviceSoapClient();
$api_token = $client->auth_request_api($tech_username, $tech_password);
$user_token = $client->auth_request_user($api_token, $username, $password);
print_r($client->search_in_namehost($user_token, $keyword));
?>

这是我在终端

中运行php test.php时得到的结果

test.php在终端输出

localhost:Documents username$ sudo php phpinfo.php 
Array
(  
    [0] => Array
        (
            [title] => computername
            [section] => Hosts
            [incident] => Test Incident - DO NOT DELETE
            [url] => https://urlToIncident
        )

)

鉴于我可以成功获取数据,我知道这个问题与suds有关,但我对PHP和使用suds的经验都很少,并且suds和我正在使用的API的文档都不如我强大我想。我很感激能得到的任何帮助。

更新 对不起,如果我没有编辑这个权利,我很想在这里发帖,但我已经取得了一些进展......

所以熟悉SOAP而不是python的同事建议我改变将参数传递给方法调用的方式。所以我做了这个并相应地编辑了上面的代码以及我得到的新错误的输出。在这一点上,我确信python正在从方法中获取信息,但不一定能够处理它的呈现方式。

正如我上面提到的那样,当你调用test.php时,终端的输出是什么样的,我计算了返回的时间,与脚本进入该方法调用后返回这个新错误所需的时间相比较它们大致相同。之前,我几乎立即得到会话过期错误。所以现在我想问题是让python处理它正确的数据,我正在努力弄清楚。

1 个答案:

答案 0 :(得分:0)

感谢同事的一些帮助,他发现问题与我正在使用的WSDL有关。 Suds正在寻找一种不存在的阵列。一旦我在WSDL中更改了该类型的所有实例,一切正常。不幸的是,我无法发布WSDL,但我很乐意尽可能地回答问题,如果有人碰巧有任何问题。