python 3 suds 缓存不工作

python 3 suds cache not working

我正在尝试编写用于通过 Python 访问 Sharepoint 的脚本。

已安装以下库:suds.jurko、ntlm.

以下代码成功,但耗时将近 20 秒:

#!/usr/bin/env python3

from suds.client import Client
from suds.transport.https import WindowsHttpAuthenticated
from suds.cache import ObjectCache

url = 'http://blah/_vti_bin/Lists.asmx?WSDL'
user = "blah"
passwd = "blah"

ntlm = WindowsHttpAuthenticated(username=user, password=passwd)
client = Client(url, transport=ntlm)

我尝试添加缓存:

oc = ObjectCache()
oc.setduration(days=10)
client = Client(url, transport=ntlm, cache=oc)

我看到 /tmp/suds 创建了,我看到下面有缓存文件,但看起来它只是在每个 运行 上创建了更多文件,而不是使用缓存文件:

-rw-r--r-- 1 pchernik smsvcs      3 Feb  5 13:27 version
-rw-r--r-- 1 pchernik smsvcs 309572 Feb  5 13:27 suds-536283349122900148-document.px
-rw-r--r-- 1 pchernik smsvcs 207647 Feb  5 13:27 suds-4765026134651708722-document.px
-rw-r--r-- 1 pchernik smsvcs  21097 Feb  5 13:27 suds-1421279777216033364-document.px
-rw-r--r-- 1 pchernik smsvcs 207644 Feb  5 13:27 suds-6437332842122298485-document.px
-rw-r--r-- 1 pchernik smsvcs 309572 Feb  5 13:27 suds-3510377615213316246-document.px
-rw-r--r-- 1 pchernik smsvcs  21097 Feb  5 13:28 suds-7540886319990993060-document.px
-rw-r--r-- 1 pchernik smsvcs 207617 Feb  5 13:30 suds-1166110448227246785-document.px
-rw-r--r-- 1 pchernik smsvcs 309548 Feb  5 13:30 suds-2848176348666425151-document.px
-rw-r--r-- 1 pchernik smsvcs  21076 Feb  5 13:31 suds-6077994449274214633-document.px

如有任何想法/建议,我们将不胜感激。

谢谢, -帕维尔

我遇到了同样的问题,请尝试将您的缓存策略设置为 1:

client = Client(url, transport=ntlm, cache=oc, cachingpolicy=1)

这将缓存您的 WSDL 对象而不是 XML 文件。

来自 suds 文档:

cachingpolicy

The caching policy, determines how data is cached. The default is 0. version 0.4+

  • 0 = XML documents such as WSDL & XSD.
  • 1 = WSDL object graph.

编辑:我重新阅读了你的问题,意识到我遗漏了一些重要的东西;您的缓存正在重新生成。我相信这是因为没有指定缓存的位置。这是来自 cache.py:

中 FileCache class 的文档

If no cache location is specified, a temporary default location will be used. Such default cache location will be shared by all FileCache instances with no explicitly specified location within the same process. The default cache location will be removed automatically on process exit unless user sets the remove_default_location_on_exit FileCache class attribute to False.

因此,即使您想使用默认缓存位置,也需要在创建缓存对象时明确定义它。这是我在代码中所做的:

    # Configure cache location and duration ('days=0' = infinite)
    cache_dir = os.path.join(os.path.abspath(os.sep), r'tmp\suds')
    self.cache = ObjectCache(cache_dir, days=0)

您也可以尝试按照 FileCache 文档中的建议设置 remove_default_location_on_exit 属性,但我没有尝试过这种方法。

你确定你用的是suds-jourko吗?它非常类似于此处描述的问题: Suds is not reusing cached WSDLs and XSDs, although I expect it to

您可以分析您的应用程序或 运行 它启用了日志记录(如链接问题中的建议)。

作为替代方案,您可以尝试 osa:https://pypi.python.org/pypi/osa/

编辑:没看到你已经安装了suds-jourko

我遇到了同样的问题,但我注意到 pypi 中的 suds-jurko 版本在 reader.py 中具有以下生成缓存文件名称的函数:

def mangle(self, name, x):                                                   
    """                                                                      
    Mangle the name by hashing the I{name} and appending I{x}.               
    @return: the mangled name.                                               
    """                                        
    h = abs(hash(name))                                                        
    return '%s-%s' % (h, x)

在 python3 hash adds a random seed to the string. This has been fixed in the current version of suds-jurko at https://bitbucket.org/jurko/suds/ 中使用 hashlib/md5 代替。你可以从那里安装它而不是 pypi 或者只是编辑你的 reader.py 文件并将其更改为

h = hashlib.md5(name.encode()).hexdigest()