在twisted中,调用 self.transport.write() 和 self.sendLine() 有什么区别?例如,以下程序的行为与我在 lineReceived 方法中调用的内容相同:
class FooProtocol(basic.LineReceiver):
delimiter = '\n'
def connectionMade(self):
self.sendLine("Foo")
def lineReceived(self, line):
self.sendLine("Echoed: " + line)
#self.transport.write("Echoed: " + line + "\n")
if __name__ == "__main__":
stdio.StandardIO(FooProtocol())
reactor.run()
Run Code Online (Sandbox Code Playgroud)
有没有更Pythonic(或扭曲......)的方法来做到这一点?
提前致谢 !
sendLine()是一种方便的方法。默认实现是:
def sendLine(self, line):
return self.transport.write(line + self.delimiter)
Run Code Online (Sandbox Code Playgroud)
sendLine()是一个稍微高级的函数。您不需要self.transport.write()直接在面向线路的协议中使用。