2013-04-30 142 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