title: Chat Server with Twisted
body: <p>I'm learning Python Twisted and this is my example about chat server from my book:</p>
  
  <pre><code>from twisted.internet.protocol import Factory
  from twisted.protocols.basic import LineReceiver
  from twisted.internet import reactor
  
  class ChatProtocol(LineReceiver):
    def __init__(self, factory):
      self.factory = factory
      self.name = None
      self.state = "REGISTER"
  
    def connectionMade(self):
      self.sendLine("What's your name?")
  
    def connectionLost(self, reason):
      if self.name in self.factory.users:
          del self.factory.users[self.name]
          self.broadcastMessage("%s has left the channel !" %(self.name))
  
    def LineReceived(self, line):
      if self.state == "REGISTER" :
          self.handle_REGISTER(line)
      else:
          self.handle_CHAT(line)
  
    def handle_REGISTER(self, name):
      if name in self.factory.users:
          self.sendLine("Name taken, choose another!")
          return
      self.sendLine("Welcome, %s !" %(name))
      self.broadcastMessage("%s has joined channel!" %(name))
      self.name = name
      self.factory.users[name]= self
      self.state = "CHAT"
  
    def handle_CHAT(self, message):
      message = " &lt;%s&gt;  %s" %(self.name, message)
      self.broadcastMessage(message)
  
    def broadcastMessage(self, message):
      for name, protocol in self.factory.users.iteritems():
          if protocol != self:
              protocol.sendLine(message)
  
  class ChatFactory(Factory):
    def __init__(self):
      self.users = {}
  
    def buildProtocol(self, addr):
      return ChatProtocol(self)
  
  reactor.listenTCP(7100, ChatFactory())
  reactor.run()
  </code></pre>
  
  <p>And I connect by: telnet localhost 7100.
  It worked and asked me: what's your name?, but when I type my name, the sever raise this error:</p>
  
  <pre><code>Unhandled Error
  Traceback (most recent call last):
    File "C:\Python27\lib\site-packages\twisted\python\log.py", line 103, in 
  callWithLogger
      return callWithContext({"system": lp}, func, *args, **kw)
    File "C:\Python27\lib\site-packages\twisted\python\log.py", line 86, in callWithContext
      return context.call({ILogContext: newCtx}, func, *args, **kw)
    File "C:\Python27\lib\site-packages\twisted\python\context.py", line 122, in callWithContext
     return self.currentContext().callWithContext(ctx, func, *args, **kw)
    File "C:\Python27\lib\site-packages\twisted\python\context.py", line 85, in callWithContext
      return func(*args,**kw)
  --- &lt;exception caught here&gt; ---
    File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite
      why = getattr(selectable, method)()
    File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 208, in doRead
      return self._dataReceived(data)
    File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 214, in _dataReceived
      rval = self.protocol.dataReceived(data)
    File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 571, in dataReceived
      why = self.lineReceived(line)
    File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 625, in lineReceived
      raise NotImplementedError
  exceptions.NotImplementedError: 
  </code></pre>
  
  <p>I'm very new to this so I really do not understand much.</p>