Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python twisted transport.write从不发送数据_Python_Twisted.client - Fatal编程技术网

python twisted transport.write从不发送数据

python twisted transport.write从不发送数据,python,twisted.client,Python,Twisted.client,我有一个服务器,它松散地基于Twisted示例聊天服务器。它与telnet一起工作。现在我需要一个客户端连接到它。我复制了示例一次性客户端。问题是服务器从未收到“hello”字符串,该字符串应用于启动“对话”。我在FAQ中读到一个常见的错误是阻止反应堆运行,但在下面的代码中,我看不出我在哪里可以做到这一点。谁能告诉我怎么了 from twisted.internet import reactor from twisted.internet.protocol import Factory, Pro

我有一个服务器,它松散地基于Twisted示例聊天服务器。它与telnet一起工作。现在我需要一个客户端连接到它。我复制了示例一次性客户端。问题是服务器从未收到“hello”字符串,该字符串应用于启动“对话”。我在FAQ中读到一个常见的错误是阻止反应堆运行,但在下面的代码中,我看不出我在哪里可以做到这一点。谁能告诉我怎么了

from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint

class Greeter(Protocol):
    def sendMessage(self, msg):
        self.transport.write("%s\n" % msg)

class GreeterFactory(Factory):
    def buildProtocol(self, addr):
        return Greeter()

def gotProtocol(p):
    p.sendMessage("hello")
    reactor.callLater(1, p.sendMessage, "/p2")
    reactor.callLater(2, p.transport.loseConnection)

point = TCP4ClientEndpoint(reactor, "localhost", 8123)
d = point.connect(GreeterFactory())
d.addCallback(gotProtocol)
reactor.run()

嗯。我希望这对其他人有帮助。我上面的问题是我的服务器(使用lineReceived):

应为回车符和换行符。因此,将上面的sendMessage更改为:

    def sendMessage(self, msg):
        self.transport.write("%s\r\n" % msg)

它现在神奇地工作了!简单得令人沮丧

您也可以发布您的服务器实现吗?
    def sendMessage(self, msg):
        self.transport.write("%s\r\n" % msg)