客户端 - Twisted,Python中的服务器聊天

时间:2014-01-07 02:07:52

标签: python twisted

当它们位于不同的终端窗口时,我无法让服务器和客户端进行通信。我可以让他们连接虽然没有实际发送他们的输出到彼此的窗口。我 客户端:

from twisted.internet import reactor, stdio, protocol

from twisted.protocols import basic


class Echo(basic.LineReceiver):

   def connectionMade(self):

       print "Welcome to the Chat, you have now connected"


  def lineReceived(self, line):

       self.sendLine(line)

       if line=="exit":

           connectionLost()



  def connectionLost(self):

       self.transport.loseConnection()


class EchoClientFactory(protocol.ClientFactory):

    protocol = Echo


factory = EchoClientFactory()

reactor.connectTCP("localhost", ...., factory)

reactor.run()

服务器:

 from twisted.internet import reactor, protocol, stdio

 from twisted.protocols import basic


 class Echo(basic.LineReceiver):

     print "Welcome to Chat"

     def connectionMade(self):

          print "A new client has connected"

     def lineReceived(self, line):

          self.sendLine(line)

          if line=="exit":

             connectionLost()


    def connectionLost(self):

       self.transport.loseConnection()



 class EchoServerFactory(protocol.ServerFactory):

    protocol = Echo


 factory = EchoServerFactory()

 reactor.listenTCP(...., factory)

 reactor.run()

1 个答案:

答案 0 :(得分:3)

始终发布正在运行的确切代码至关重要 - 您的Echo Server指定....作为执行时引发语法错误的端口。发布确切的代码意味着您可以更快地获得更好的响应。

使用数字9999替换端口,允许服务器代码运行。现在我们可以通过telnet或netcat连接来测试服务器:

$ nc -c localhost 9999
hello
hello

大!服务器工作正常。请注意,键入“exit”时会出错:

exceptions.NameError: global name 'loseConnection' is not defined

如果要手动删除连接,则应该调用self.transport.loseConnection()。然后,您定义的connectionLost方法将被调用为允许您响应的事件。您现在不需要在此阶段定义该方法。以下是服务器代码的修改版本,其中包含建议的更改:

from twisted.internet import reactor, protocol, stdio
from twisted.protocols import basic

class Echo(basic.LineReceiver):
    print "Welcome to Chat"

    def connectionMade(self):
        print "A new client has connected"

    def lineReceived(self, line):
        print 'server received:', line
        print 'server sent:', line, '\n'
        self.sendLine(line)
        if line=="exit":
             self.transport.loseConnection()


class EchoServerFactory(protocol.ServerFactory):
   protocol = Echo


factory = EchoServerFactory()
reactor.listenTCP(9999, factory)
reactor.run()

客户端与端口有同样的问题,更改为9999允许它运行。您的客户端连接,但随后不发送任何数据。这是一个版本,它在连接时发送一些文本,并在延迟2秒后回显文本:

from twisted.internet import reactor, stdio, protocol
from twisted.protocols import basic

class Echo(basic.LineReceiver):
    def connectionMade(self):
        print "Welcome to the Chat, you have now connected"

        # send some text when we connect
        self.sendLine('hello')

    def lineReceived(self, line):
        print 'client received:', line

        if len(line) > 10:
            self.sendLine('exit')
        else:
            # send a response in 2 seconds
            reactor.callLater(2, self.sendLine, '>' + line)

    def connectionLost(self, reason):
        reactor.stop()


class EchoClientFactory(protocol.ClientFactory):
    protocol = Echo

factory = EchoClientFactory()
reactor.connectTCP("localhost", 9999, factory)
reactor.run()

这会导致原始邮件反弹并转发到服务器,而客户端每次都会预先添加>个字符。然后,当消息达到一定长度时,客户端将发送“退出”,导致服务器删除连接。当连接断开时,客户端可以停止其反应器以便退出。

键入终端窗口不会通过客户端向服务器发送数据 - 为此目的使用telnet或netcat。