-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonChatServer.py
More file actions
168 lines (147 loc) · 6.43 KB
/
PythonChatServer.py
File metadata and controls
168 lines (147 loc) · 6.43 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python
import socketserver
import re
import socket
class ClientError(Exception):
"An exception thrown because the client gave bad input to the server."
pass
class PythonChatServer(socketserver.ThreadingTCPServer):
"The server class."
def __init__(self, server_address, RequestHandlerClass):
"""Set up an initially empty mapping between a user's nickname
and the file-like object used to send data to that user."""
SocketServer.ThreadingTCPServer.__init__(self, server_address,
RequestHandlerClass)
self.users = {}
class RequestHandler(SocketServer.StreamRequestHandler):
"""Handles the life cycle of a user's connection to the chat
server: connecting, chatting, running server commands, and
disconnecting."""
NICKNAME = re.compile('^[A-Za-z0-9_-]+$') #Regex for a valid nickname
def handle(self):
"""Handles a connection: gets the user's nickname, then
processes input from the user until they quit or drop the
connection."""
self.nickname = None
self.privateMessage('Who are you?')
nickname = self._readline()
done = False
try:
self.nickCommand(nickname)
self.privateMessage('Hello %s, welcome to the Python Chat Server.'\
% nickname)
self.broadcast('%s has joined the chat.' % nickname, False)
except ClientError (error):
self.privateMessage(error.args[0])
done = True
except socket.error:
done = True
#Now they're logged in; let them chat.
while not done:
try:
done = self.processInput()
except ClientError (error):
self.privateMessage(str(error))
except socket.error (e):
done = True
def finish(self):
"Automatically called when handle() is done."
if self.nickname:
#The user successfully connected before disconnecting.
#Broadcast that they're quitting to everyone else.
message = '%s has quit.' % self.nickname
if hasattr(self, 'partingWords'):
message = '%s has quit: %s' % (self.nickname,
self.partingWords)
self.broadcast(message, False)
#Remove the user from the list so we don't keep trying to
#send them messages.
if self.server.users.get(self.nickname):
del(self.server.users[self.nickname])
self.request.shutdown(2)
self.request.close()
def processInput(self):
"""Reads a line from the socket input and either runs it as a
command, or broadcasts it as chat text."""
done = False
l = self._readline()
command, arg = self._parseCommand(l)
if command:
done = command(arg)
else:
l = '<%s> %s\n' % (self.nickname, l)
self.broadcast(l)
return done
Each server command is implemented as a method. The _parseCommand method, defined later, takes a line that looks like /nick and calls the corresponding method (in this case, nickCommand):
#Below are implementations of the server commands.
def nickCommand(self, nickname):
"Attempts to change a user's nickname."
if not nickname:
raise ClientError ('No nickname provided.')
if not self.NICKNAME.match(nickname):
raise ClientError (Invalid nickname: %s' % nickname)
if nickname == self.nickname:
raise ClientError ('You are already known as %s.' % nickname)
if self.server.users.get(nickname, None):
raise ClientError ('There\'s already a user named "%s" here.' % nickname)
oldNickname = None
if self.nickname:
oldNickname = self.nickname
del(self.server.users[self.nickname])
self.server.users[nickname] = self.wfile
self.nickname = nickname
if oldNickname:
self.broadcast('%s is now known as %s' % (oldNickname, self.nickname))
def quitCommand(self, partingWords):
"""Tells the other users that this user has quit, then makes
sure the handler will close this connection."""
if partingWords:
self.partingWords = partingWords
#Returning True makes sure the user will be disconnected.
return True
def namesCommand(self, ignored):
"Returns a list of the users in this chat room."
self.privateMessage(', '.join(self.server.users.keys()))
# Below are helper methods.
def broadcast(self, message, includeThisUser=True):
"""Send a message to every connected user, possibly exempting the
user who's the cause of the message."""
message = self._ensureNewline(message)
for user, output in self.server.users.items():
if includeThisUser or user != self.nickname:
output.write(message)
def privateMessage(self, message):
"Send a private message to this user."
self.wfile.write(self._ensureNewline(message))
def _readline(self):
"Reads a line, removing any whitespace."
return self.rfile.readline().strip()
def _ensureNewline(self, s):
"Makes sure a string ends in a newline."
if s and s[-1] != '\n':
s += '\r\n'
return s
def _parseCommand(self, input):
"""Try to parse a string as a command to the server. If it's an
implemented command, run the corresponding method."""
commandMethod, arg = None, None
if input and input[0] == '/':
if len(input) < 2:
raise ClientError, 'Invalid command: "%s"' % input
commandAndArg = input[1:].split(' ', 1)
if len(commandAndArg) == 2:
command, arg = commandAndArg
else:
command, = commandAndArg
commandMethod = getattr(self, command + 'Command', None)
if not commandMethod:
raise ClientError, 'No such command: "%s"' % command
return commandMethod, arg
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print('Usage: %s [hostname] [port number]' % sys.argv[0])
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
PythonChatServer((hostname, port), RequestHandler).serve_forever()