-
Notifications
You must be signed in to change notification settings - Fork 194
/
TwistedChat.py
47 lines (39 loc) · 1.24 KB
/
TwistedChat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class NonAnonChat(LineReceiver):
def __init__(self, protocols):
self.users = {'john':'john', 'adastra':'adastra'}
self.userName = None
self.userLogged = False
self.protocols = protocols
def connectionMade(self):
self.sendLine('Your Username: ')
def connectionLost(self, reason):
for protocol in self.protocols:
if protocol != self:
protocol.sendLine('Connection Lost: %s '%(reason))
def lineReceived(self, line):
if self.userName == None:
if self.users.has_key(line):
self.userName = line
self.sendLine('Password: ')
else:
self.sendLine('Wrong Username')
elif self.userLogged == False:
if self.users[self.userName] == line:
self.userLogged = True
self.protocols.append(self)
else:
self.sendLine('Wrong Password')
elif self.userLogged == True:
for protocol in self.protocols:
if protocol != self:
protocol.sendLine('%s Said: %s ' %(self.userName, line))
class NonAnonFactory(Factory):
def __init__(self):
self.protocols = []
def buildProtocol(self, addr):
return NonAnonChat(self.protocols)
reactor.listenTCP(8000, NonAnonFactory())
reactor.run()