我一直在努力制作一个sftp服务器,并且无法从ISFTPServer.openDirectory函数返回任何信息。



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})}


有人知道发生了什么或我做错了什么吗?

最佳答案

您对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})


现在,您有了一个生成器-这是一个带有close方法的迭代器-其元素为三元组,如文档中所述。

关于python - 扭曲的Python ISFTPServer openDirectory,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16300262/

10-10 02:21