I was learning python for a few weeks, I was trying to build a chat app, I chose twisted and felt comfortable with it, but I run into a strange problem in client tesing code.
Here is my server code:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class ChatServer(LineReceiver):
def __init__(self, users):
self.users = users
self.name = None
self.state = "GETNAME"
def connectionMade(self):
self.sendLine("What's your name?")
print 'DEBUG: Name Input Request Send Back To Client.'
def connectionLost(self, reason):
if self.users.has_key(self.name):
del self.users[self.name]
def lineReceived(self, line):
print 'DEBUG: A Message Recieved From Client.'
if self.state == "GETNAME":
self.handle_GETNAME(line)
else:
self.handle_CHAT(line)
def handle_GETNAME(self, name):
if self.users.has_key(name):
self.sendLine("Name taken, please choose another.")
return
self.sendLine("Welcome, %s!" % (name,))
print 'DEBUG: Welcome Message Send Back To Client.'
self.name = name
self.users[name] = self
self.state = "CHAT"
def handle_CHAT(self, message):
message = "<%s> %s" % (self.name, message)
for name, protocol in self.users.iteritems():
if protocol != self:
protocol.sendLine(message)
class ChatServerFactory(Factory):
def __init__(self):
self.users = {}
def buildProtocol(self, addr):
return ChatServer(self.users)
reactor.listenTCP(8123, ChatServerFactory())
reactor.run()
and this is my client testing code, I tried to test server in telnet way.
import telnetlib
import cmd
import sys
import time
def test_telnet(Host, Port):
# connected to the server
tn = telnetlib.Telnet(Host, Port)
print 'Client Connecton Made.'
# tn.set_debuglevel(2)
# read back from server
greeting = tn.read_until("What's your name?")
print 'Recieved Name Input Request From Server'
print greeting
print "DEBUG: Before telnet write data"
username = 'amber'
tn.write(username + '\n')
print "DEBUG: After telnet write data"
# welcome = tn.read_until('see', 5)
# print 'Recieved Welcome Message From Server'
# print welcome
time.sleep(7)
tn.write("exit\n")
print 'Client Connection Lost.'
test_telnet('localhost', 8123)
However, tn.write() send message, but in server console, I noticed that lineReceived is never get called, but in real telnet, it's called, why is that? Many thanks!
LineReceiver
expects lines end with \r\n
, not \n
.
Replacing \n
in the code with \r\n
solves the problem.
Alternatively you can change the line delimiter:
class ChatServer(LineReceiver):
delimiter = '\n' # <----
...