-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
139 lines (103 loc) · 3.79 KB
/
server.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
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
import asyncore
import socket
import shlex
class EchoHandler(asyncore.dispatcher_with_send):
def __init__(self, ident, *args, **kwargs):
# call superclass init
asyncore.dispatcher_with_send.__init__(self, *args, **kwargs)
# set ident and name
self.ident = ident
self.nick = str(ident)
def handle_read(self):
data = self.recv(8192)
methods = {"help": self.print_help,
"nick": self.set_nick,
"list": self.list_players,
"say" : self.say}
if data:
message = shlex.split(data)
if message[0] not in methods:
self.send("command %s not found\n\n" % message[0])
else:
methods[message[0]](message)
def print_help(self, *args, **kwds):
message = r"""
USAGE:
help ...................... print out help page
list ...................... list players
nick ...................... show your own nick
nick Larry ................ set your nick to "Larry"
buzzer .................... press the buzzer button
say Hello ................. say "Hello" to the people
"""
self.send(message)
def set_nick(self, *args, **kwds):
if len(args[0]) == 1:
if str(self.ident) == self.nick:
self.send('first set your nickname with "nick nickname"\n\n')
else:
self.send("your nick is: %s\n\n" % self.nick)
else:
self.nick="%s(%s)" % (" ".join(args[0][1:]), self.ident[1])
self.send('your nick was changed to: %s\n\n' % self.nick)
def list_players(self, *args, **kwds):
self.send(", ".join(server.get_clients())+"\n\n")
def say(self, *args, **kwds):
if len(args[0]) > 1:
if str(self.ident) == self.nick:
self.send('first set your nickname with "nick nickname"\n\n')
else:
message = " ".join(args[0][1:])
server.broadcast("%s: %s\n" % (self.nick, message))
else:
self.send("(said nothing)\n\n")
class EchoServer(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(5)
self.connections = []
def broadcast(self, text, newline=True):
for client in self.connections:
try:
client.send(text+"\n" if newline else "")
except:
pass
def get_clients(self):
clients = []
for client in self.connections:
try:
if client.nick == str(client.ident):
clients.append("no_nick_set!(%s)" % client.ident[1])
else:
clients.append(client.nick)
except:
pass
return clients
def handle_accept(self):
pair = self.accept()
if pair is None:
pass
else:
sock, addr = pair
print 'Incoming connection from %s' % repr(addr)
greeting = r"""
Welcome to the Quizshow!
========================================
_____________________________________
/ Set your nick with "nick Larry" and \
\ list the help page with "help" /
-------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
"""
sock.send(greeting)
handler = EchoHandler(addr, sock)
self.connections.append(handler)
server = EchoServer('localhost', 8080)
asyncore.loop()