2015-02-05 25 views
1

我試圖寫一個腳本通過Python來訪問SharePoint。蟒蛇3個肥皂水緩存不工作

以下庫已安裝: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 /肥皂水和我看到在有緩存的文件,但看起來它只是在每次運行時創建更多文件,而不是使用緩存文件:

-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 
  • 泡沫通常會這麼慢嗎?
  • 關於修復緩存問題的任何想法?
  • 是否有任何其他的Python 3個庫,我可以使用這個,而不是泡沫?

任何意見/建議表示讚賞。

感謝, -Pavel

回答

1

我有同樣的問題,試試你的cachingpolicy設置爲1:

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

這將緩存您的WSDL對象,而不是你的XML文件。

從泡沫文檔:

cachingpolicy

的緩存策略,決定數據緩存。默認值爲0.版本0.4+

  • 0 = XML文檔,如WSDL & XSD。
  • 1 = WSDL對象圖。

編輯:我重讀你的問題,並意識到我缺少一些重要的東西;您的緩存正在重新生成。我相信這是由於未指定緩存的位置。這來自cache.py中的FileCache類的文檔:

如果未指定緩存位置,則會使用臨時默認位置 。這種默認的緩存位置將由所有FileCache 情況下,用相同的 過程中沒有明確指定位置共享。 默認的緩存位置將被自動上 進程退出,除非用戶設置了remove_default_location_on_exit FileCache類屬性設置爲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) 

您也可以嘗試設置remove_default_location_on_exit屬性作爲FileCache文檔中的建議,但我還沒有嘗試過這種方法。

+0

感謝您的建議David。我其實之前嘗試過,沒有運氣 - 看起來不像將設置更改爲1或0對我有任何影響。 – 2015-02-06 22:09:26

0

我有同樣的問題,但我注意到在PyPI中無泡沫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字符串。這已通過使用hashlib/md5代替當前版本的suds-jurko在https://bitbucket.org/jurko/suds/中修復。你可以從那裏安裝它而不是pypi,或者只是編輯你的reader.py文件並將其更改爲

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