问题是,在您关闭连接之前,Twisted 似乎不会发送任何内容。该问题在我的客户端和 Firefox 上都可见(服务器未发送)。
这是完整的代码。
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from twisted.internet.protocol import Protocol,Factory
from twisted.internet.endpoints import TCP4ServerEndpoint,TCP4ClientEndpoint
from twisted.internet import reactor
import thread
class echoProtocol(Protocol):
def dataReceived(self,data):
self.transport.write(data+"\n - Server")
class echoFactory(Factory):
def buildProtocol(self,addr):
print addr.host
return echoProtocol()
class clientProtocol(Protocol):
def sendMessage(self,message):
self.transport.write(message)
def dataReceived(self,data):
print data
class clientFactory(Factory):
def buildProtocol(self,addr):
return clientProtocol()
def messageLoop(p):
while 1 :
text=raw_input("")
p.sendMessage(text)
def connectedProtocol(p):
thread.start_new_thread(messageLoop, p)
if __name__ == '__main__':
choice=raw_input("Server?[y/n]")
if choice.lower()=="y":
TCP4ServerEndpoint(reactor,44554).listen(echoFactory())
reactor.run()
else:
TCP4ClientEndpoint(reactor,"127.0.0.1",44554).connect(clientFactory()).addCallback(connectedProtocol)
reactor.run()
如何让 Twisted 在关闭连接之前实际发送一些东西?