1 | #!/usr/bin/python
|
---|
2 | # First the server
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 | """
|
---|
7 | A basic, multiclient 'chat server' using Python's select module
|
---|
8 | with interrupt handling.
|
---|
9 |
|
---|
10 | Entering any line of input at the terminal will exit the server.
|
---|
11 | """
|
---|
12 |
|
---|
13 | import select
|
---|
14 | import socket
|
---|
15 | import sys
|
---|
16 | import signal
|
---|
17 | from communication import send, receive
|
---|
18 | import threading
|
---|
19 | import GlobalVariables
|
---|
20 | BUFSIZ = 1024
|
---|
21 |
|
---|
22 |
|
---|
23 | class CommServer(threading.Thread):
|
---|
24 | """ Simple server using select """
|
---|
25 |
|
---|
26 | def __init__(self, port=3490, backlog=5):
|
---|
27 | self.clients = 0
|
---|
28 | # Client map
|
---|
29 | self.clientmap = {}
|
---|
30 | # Output socket list
|
---|
31 | self.outputs = []
|
---|
32 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
---|
33 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
---|
34 | self.server.bind(('',port))
|
---|
35 | print 'Listening to port',port,'...'
|
---|
36 | self.server.listen(backlog)
|
---|
37 | # Trap keyboard interrupts
|
---|
38 | #signal.signal(signal.SIGINT, self.sighandler)
|
---|
39 |
|
---|
40 | def sighandler(self, signum, frame):
|
---|
41 | # Close the server
|
---|
42 | print 'Shutting down server...'
|
---|
43 | # Close existing client sockets
|
---|
44 | for o in self.outputs:
|
---|
45 | o.close()
|
---|
46 |
|
---|
47 | self.server.close()
|
---|
48 |
|
---|
49 | def getname(self, client):
|
---|
50 |
|
---|
51 | # Return the printable name of the
|
---|
52 | # client, given its socket...
|
---|
53 | info = self.clientmap[client]
|
---|
54 | host, name = info[0][0], info[1]
|
---|
55 | return '@'.join((name, host))
|
---|
56 |
|
---|
57 | def serve(self):
|
---|
58 |
|
---|
59 | inputs = [self.server,sys.stdin]
|
---|
60 | self.outputs = []
|
---|
61 |
|
---|
62 | running = 1
|
---|
63 |
|
---|
64 | while running:
|
---|
65 |
|
---|
66 | try:
|
---|
67 | inputready,outputready,exceptready = select.select(inputs, self.outputs, [])
|
---|
68 | except select.error, e:
|
---|
69 | break
|
---|
70 | except socket.error, e:
|
---|
71 | break
|
---|
72 |
|
---|
73 | for s in inputready:
|
---|
74 |
|
---|
75 | if s == self.server:
|
---|
76 | # handle the server socket
|
---|
77 | client, address = self.server.accept()
|
---|
78 | print 'server: got connection %d from %s' % (client.fileno(), address)
|
---|
79 | # Read the login name
|
---|
80 | cname = receive(client).split('NAME: ')[1]
|
---|
81 |
|
---|
82 | # Compute client name and send back
|
---|
83 | self.clients += 1
|
---|
84 | send(client, 'CLIENT: ' + str(address[0]))
|
---|
85 | inputs.append(client)
|
---|
86 |
|
---|
87 | self.clientmap[client] = (address, cname)
|
---|
88 | # Send joining information to other clients
|
---|
89 | msg = '\n(Connected: New client (%d) from %s)' % (self.clients, self.getname(client))
|
---|
90 | for o in self.outputs:
|
---|
91 | # o.send(msg)
|
---|
92 | send(o, msg)
|
---|
93 |
|
---|
94 | self.outputs.append(client)
|
---|
95 |
|
---|
96 | #elif s == sys.stdin:
|
---|
97 | # handle standard input
|
---|
98 | # junk = sys.stdin.readline()
|
---|
99 | # if (junk[:-1]=='exit'):
|
---|
100 | # running = 0
|
---|
101 | elif s!=sys.stdin:
|
---|
102 | # handle all other sockets
|
---|
103 | try:
|
---|
104 | # data = s.recv(BUFSIZ)
|
---|
105 | data = receive(s)
|
---|
106 | if data:
|
---|
107 | # Send as new client's message...
|
---|
108 | msg = 'SERVER: You sent: >> ' + data
|
---|
109 | GlobalVariables.UserInput=data
|
---|
110 | if(GlobalVariables.UserInput=='exit' or GlobalVariables.UserInput=='EXIT'):
|
---|
111 | running=0
|
---|
112 | print 'Shutting down server...'
|
---|
113 | # Close existing client sockets
|
---|
114 | for o in self.outputs:
|
---|
115 | o.close()
|
---|
116 |
|
---|
117 | # Send data to all except ourselves
|
---|
118 | for o in self.outputs:
|
---|
119 | #if o != s:
|
---|
120 | # o.send(msg)
|
---|
121 | send(o, msg)
|
---|
122 | else:
|
---|
123 | print 'server: %d hung up' % s.fileno()
|
---|
124 | self.clients -= 1
|
---|
125 | s.close()
|
---|
126 | inputs.remove(s)
|
---|
127 | self.outputs.remove(s)
|
---|
128 |
|
---|
129 | # Send client leaving information to others
|
---|
130 | msg = '\n(Hung up: Client from %s)' % self.getname(s)
|
---|
131 | for o in self.outputs:
|
---|
132 | # o.send(msg)
|
---|
133 | send(o, msg)
|
---|
134 |
|
---|
135 | except socket.error, e:
|
---|
136 | # Remove
|
---|
137 | inputs.remove(s)
|
---|
138 | self.outputs.remove(s)
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | self.server.close()
|
---|
143 | def run(self):
|
---|
144 | self.serve()
|
---|
145 |
|
---|
146 | if __name__ == "__main__":
|
---|
147 | commServer=CommServer()
|
---|
148 | commServer().start()
|
---|