【问题标题】:Having trouble with a simple Twisted chat server使用简单的 Twisted 聊天服务器时遇到问题
【发布时间】:2015-03-27 19:01:33
【问题描述】:

当我尝试运行此程序时(请参见下面的代码),我从服务器和命令提示符处获得“建立连接”响应以写入输入。但是,当我尝试输入输入时,它只是挂起,服务器似乎没有收到消息。有谁知道这是为什么?

谢谢,如果这还不够清楚,请说出来

这是我的聊天服务器:

from twisted.protocols import basic



class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + '\n')


from twisted.internet import reactor, protocol
from twisted.application import service, internet

factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
reactor.listenTCP(8004, factory)
reactor.run()

这是我的客户:

from twisted.internet import reactor, protocol


# a client protocol

class EchoClient(protocol.Protocol):

    def sendData(self):
        data = raw_input("> ")
        if data:
            print "sending %s...." % data
            self.transport.write(data)
        else:
            self.transport.loseConnection()

    def connectionMade(self):
        self.sendData()

    def dataReceived(self, data):
        print data
        self.sendData()

    def connectionLost(self, reason):
        print "connection lost"

class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()


# this connects the protocol to a server runing on port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 8004, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python client chat twisted server


    【解决方案1】:

    您在客户端中犯了一个错误。基本上,服务器期望接收行,这意味着数据由换行符终止。但是,客户端发送的数据最后没有换行符。

    所以,要修复客户端,只需将\r\n 添加到数据中:

    self.transport.write(data + "\r\n")
    

    这是客户端协议:

    class EchoClient(protocol.Protocol):
    
        def sendData(self):
            data = raw_input("> ")
            if data:
                print "sending %s...." % data
                self.transport.write(data + "\r\n")
            else:
                self.transport.loseConnection()
    
        def connectionMade(self):
            self.sendData()
    
        def dataReceived(self, data):
            print data
            self.sendData()
    
        def connectionLost(self, reason):
            print "connection lost"
    

    【讨论】:

    • 谢谢队友真的很有帮助:-)
    猜你喜欢
    • 2015-03-11
    • 2012-10-27
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多