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