python - 扭曲的 python ISFTPServer openDirectory

标签 python twisted

我一直在通过twisted 来制作一个sftp 服务器,但无法从我的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})}

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

最佳答案

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

返回三个元素的元组。来自接口(interface)文档:

    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/

相关文章:

Python PyEnchant 段错误或无效指针

python - 程序输出错误的输出

python - 在没有pygame的情况下下载pygame游戏?

python - 每天在python中创建一个日志文件

javascript - Twisted 的 Deferred 和 JavaScript 中的 Promise 一样吗?

python - numba 的表现(我做错了什么吗?)

python - 在 Twisted 中发出头部请求

proxy - 如何从 Twisted 中的 XML-RPC 回调发送 XML-RPC 请求

python - 如何使用 twisted 监听多个 udp 端口​​?

python - 高速公路从外部应用程序发送用户特定和广播消息