关于扭曲文档

1 投票
1 回答
517 浏览
提问于 2025-04-16 21:58

我刚开始学习Twisted。最近,我在读一本叫《Twisted网络编程基础》的书。书中的示例2-3如下:

class QuickDisconnectProtocol(protocol.Protocol): 

def connectionMade(self): 

    print "Connected to %s." % self.transport.getPeer( ).host 

    self.transport.loseConnection( ) 

==================================

self.transport.loseConnection( )

请问“transport”这个成员变量在哪里呢?我在Protocol里找不到它。

在示例2-4中也是同样的问题……

有没有人能告诉我怎么阅读Twisted的文档?谢谢!

1 个回答

1
def makeConnection(self, transport): ([source][1])
    """
    overridden in twisted.protocols.amp.BinaryBoxProtocol,
    twisted.protocols.ftp.ProtocolWrapper, twisted.protocols.ftp.SenderProtocol, 
    twisted.protocols.policies.ProtocolWrapper, 
    twisted.protocols.stateful.StatefulProtocol`

    Make a connection to a transport and a server.
    This sets the 'transport' attribute of this Protocol, and calls the connectionMade() 
    callback.
    """

传输就是你用来连接其他东西的方式,比如 telnet、SSH、文件等等。你可以在网上查找关于 transport 的API文档,看看具体的内容。

http://twistedmatrix.com/documents/8.2.0/api/twisted.conch.ssh.transport.SSHTransportBase.html

这里有一些现有的传输方式,来自于 http://twistedmatrix.com/documents/8.2.0/api/twisted.internet.interfaces.ITransport.html

Known subclasses: twisted.conch.insults.insults.ITerminalTransport, 
twisted.conch.telnet.ITelnetTransport, twisted.internet.interfaces.IProcessTransport, 
twisted.internet.interfaces.ITCPTransport

Known implementations: twisted.conch.ssh.channel.SSHChannel, 
twisted.internet._posixstdio.StandardIO, twisted.internet._win32stdio.StandardIO, 
twisted.internet.abstract.FileDescriptor, twisted.internet.iocpreactor.abstract.FileHandle, 
twisted.internet.protocol.FileWrapper, twisted.protocols.loopback._LoopbackTransport, 
twisted.protocols.loopback.LoopbackRelay

根据你想连接的地方,你在调用 makeConnection(transport) 时会使用其中一种方式,当你这样做的时候,它就成为协议的一个属性了。

撰写回答