Не смеяцо, надо закончить уже 4й курс)))

 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
class ClientThread(threading.Thread):
def __init__(self,sock,addr,key,buf_size):
self.sock=sock
self.sock.settimeout(60)
self.addr=addr
self.key=key
self.buf_size=buf_size
threading.Thread.__init__(self)
def run(self):
data=self.sock.recv(self.buf_size)
if data==self.key:
self.sock.send(md5.new(self.key).hexdigest())
else:
self.sock.close()
print u'Client disconnected, wrong key: %s, %s' % (self.addr[0],self.addr[1])
return
ServerSocket.startevent.wait()
print 'Game started from %s' % self.getName()
class ServerSocket(socket.socket):
startevent=threading.Event()
def __init__(self):
socket.socket.__init__(self, socket.AF_INET, socket.SOCK_STREAM)
config = ConfigParser.ConfigParser()
config.read('config.ini')
self.key=config.get('auth', 'key', 1)
self.buf_size=int(config.get('main', 'buf_size', 1))
self.host=config.get('server', 'host', 1)
self.port=int(config.get('server', 'port', 1))
self.maxclient=int(config.get('server', 'maxclient', 1))
self.bind((self.host,self.port))
self.listen(self.maxclient)
self.clientnum=0
def accept_conn(self,sock,addr):
print u'Client connected: %s, %s' % (addr[0],addr[1])
conn=ClientThread(sock,addr,self.key,self.buf_size)
conn.start()
self.clientnum+=1
if self.clientnum>1: ServerSocket.startevent.set()