-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
51 lines (38 loc) · 1.16 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
# -*- coding:utf-8 -*-
import asyncore
from ChatSession import ChatSession
from Hall import Hall
class ChatHandler(asyncore.dispatcher_with_send):
def __init__(self,socket):
super(ChatHandler,self).__init__(socket)
self.size_buff = 1024
# 可读时调用
def handle_read(self):
data = self.recv(self.size_buff)
if not data:
self.close()
else:
pass
# 连接关闭时调用
def handle_close(self):
print("关闭")
self.close()
class ChatServer(asyncore.dispatcher):
def __init__(self,host,port,listen_number):
super(ChatServer,self).__init__()
self.create_socket()
self.set_reuse_addr()
self.bind((host,port))
self.listen(listen_number)
self.chat_session = ChatSession()
self.hall = Hall()
def handle_accepted(self, sock, addr):
print("{addr}连接进来".format(addr = addr))
handler = ChatHandler(sock)
self.hall.Login(sock)
if __name__ == "__main__":
host = "127.0.0.1"
port = 8087
listen_number = 10
server = ChatServer(host,port,listen_number)
asyncore.loop()