2013-04-30 87 views
1

我一直在通過扭曲來製作一個sftp服務器,並且無法從我的ISFTPServer.openDirectory函數返回任何信息。扭曲蟒蛇ISFTPServer openDirectory

EX

class MySFTPAdapter: 
    implements(filetransfer.ISFTPServer) 
    def openDirectory(self, path): 
     return ('test', 'drwxrwxrwx 1 ab  cd    0 Apr 23 15:41 test', {'size': 0, 'uid': 1000, 'gid': 1000, 'mtime': 1366746069L, 'atime': 1366746069L, 'permissions': 511}) 

Traceback (most recent call last): 
    File "sftpserver.py", line 435, in dataReceived 
    f(data) 
    File "/usr/lib/python2.6/dist-packages/twisted/conch/ssh/filetransfer.py", line 265, in packet_OPENDIR 
    d.addCallback(self._cbOpenDirectory, requestId) 
    File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 260, in addCallback 
    callbackKeywords=kw) 
    File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 249, in addCallbacks 
    self._runCallbacks() 
--- <exception caught here> --- 
    File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 441, in _runCallbacks 
    self.result = callback(self.result, *args, **kw) 
    File "/usr/lib/python2.6/dist-packages/twisted/conch/ssh/filetransfer.py", line 269, in _cbOpenDirectory 
    handle = str(hash(dirObj)) 
exceptions.TypeError: unhashable type: 'dict' 

的異常幀當地人失敗;

{'val': ('test', 'drwxrwxrwx 1 ab  cd    0 Apr 23 15:41 test', {'size': 0, 'uid': 1000, 'gid': 1000, 'mtime': 1366746069L, 'atime': 1366746069L, 'permissions': 511})} 

任何人都有一個想法怎麼了,我做錯了什麼?

回答

3

你實現openDirectory

def openDirectory(self, path): 
    return ('test', 
      'drwxrwxrwx 1 ab  cd    0 Apr 23 15:41 test', 
      {'size': 0, 'uid': 1000, 'gid': 1000, 'mtime': 1366746069L, 
      'atime': 1366746069L, 'permissions': 511}) 

返回三個元素的元組。從接口文檔:

This method returns an iterable object that has a close() method,                  
    or a Deferred that is called back with same.                       

    The close() method is called when the client is finished reading                  
    from the directory. At this point, the iterable will no longer                   
    be used.                                

    The iterable should return triples of the form (filename,                    
    longname, attrs) or Deferreds that return the same. The                    
    sequence must support __getitem__, but otherwise may be any                    
    'sequence-like' object.              

你返回的元組聽起來像一個迭代器元素這裏正在討論的,而不是整個返回值。

試着這麼做:

def openDirectory(self, path): 
    yield ('test', 
      'drwxrwxrwx 1 ab  cd    0 Apr 23 15:41 test', 
      {'size': 0, 'uid': 1000, 'gid': 1000, 'mtime': 1366746069L, 
      'atime': 1366746069L, 'permissions': 511}) 

現在你有一臺發電機 - 這是一個接近方法的迭代器 - 它的元素,如文檔中描述的三元組。

+0

*編輯確定沒有工作。 你知道我可以用@ defer.inlineCallbacks /的裝飾器來做到這一點嗎? 另外,對不起,我不能upvote你,因爲我沒有足夠的聲譽 – somepoortech 2013-04-30 20:58:20

+0

我懷疑你想用'inlineCallbacks'做什麼是不容易的。當用'inlineCallbacks'裝飾一個函數時,它將變成一個函數,它返回一個'Deferred',它會在生成器的末尾提供一個值。但是,如果您只是在不使用'inlineCallbacks'的情況下生成Deferreds *,則可能會接近您想要的行爲。仔細閱讀界面文檔,並特別注意它允許您發送延期的地方。對界面內置的Deferreds的支持可能足以讓您的工作變得輕鬆。 – 2013-05-01 11:35:08