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