-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSocketChatroom_windows.py
176 lines (161 loc) · 5.21 KB
/
SocketChatroom_windows.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#coding=utf-8
import sys
import socket
import select
import threading
import argparse
def readFromStdin(s):
while 1:
data = sys.stdin.readline()
if data.lower().startswith("quit"):
s.stopFlag = True
break
else:
for client in s.outputs:
client.sendall("Server : "+data)
class ChatroomServer(object):
"""docstring for ChatroomServer"""
def __init__(self, host , port):
self.host = host
self.port = port
self.stopFlag = False
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.inputs = [self.s]
self.outputs = []
self.clients = {}
def run(self):
self.s.bind((self.host,self.port))
self.s.listen(100)
print unicode("Server is running ... ","utf-8")
threading.Thread(target=readFromStdin,args=(self,)).start()
while 1:
try:
if self.stopFlag:
print unicode("Server is close ...","utf-8")
break
readable,writeable,exceptional = select.select(self.inputs,self.outputs,[])
for sock in readable:
if sock == self.s:
clientsock,clientaddr = sock.accept()
recvname = clientsock.recv(1024)
if recvname.startswith("NAME:"):
clientname = recvname.split('NAME:')[1]
else:
clientname = str(clientaddr)
clientsock.sendall("Welcome " + clientname + "\n")
# print unicode(clientname + " Come In","utf-8")
print clientname
self.clients[clientsock] = (clientname,clientaddr,clientsock)
self.inputs.append(clientsock)
for output in self.outputs:
output.sendall("Welcome " + clientname + " Come In \n")
self.outputs.append(clientsock)
elif sock == 0:
message = sys.stdin.readline()
if message.startswith("QUIT"):
print unicode("Server is close ... ","utf-8")
sys.exit(0)
for output in self.outputs:
output.sendall("Server : " + message)
else:
try:
data = sock.recv(1024)
except Exception,e:
print unicode("falied %s"%e.message,"utf-8")
name = self.clients[sock][0]
# print unicode(name+" leaved ","utf-8")
print name+" leaved "
self.inputs.remove(sock)
self.outputs.remove(sock)
for output in self.outputs:
output.sendall(name+" leaved \n")
del self.clients[sock]
if data:
if data.startswith("SECRECT"):
print unicode("SECRECT " + self.clients[sock][0] + " : " + data,"utf-8")
output = data.split(" ")[1]
message = data.split(" ")[2]
for client in self.clients.values():
if client[0] == output:
client[2].sendall("SECRECT " + self.clients[sock][0] + " : " + message)
else:
# print unicode(self.clients[sock][0] + " : " + data,"utf-8")
print self.clients[sock][0] + " : " + data
for output in self.outputs:
if output != sock:
output.sendall(self.clients[sock][0] + " : " + data)
else:
name = self.clients[sock][0]
# print unicode(name+" leaved ","utf-8")
print name+" leaved "
self.inputs.remove(sock)
self.outputs.remove(sock)
for output in self.outputs:
output.sendall(name+" leaved \n")
del self.clients[sock]
except KeyboardInterrupt:
print unicode("Server is close ... ","utf-8")
break
def close(self):
self.s.close()
class ChatroomClient(object):
"""docstring for ChatroomClient"""
def __init__(self,host,port,name=None):
self.host = host
self.port = port
self.name = name
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
def run(self):
self.s.connect((host,port))
if self.name!=None:
self.s.sendall("NAME:"+self.name)
else:
self.s.sendall("Anonymous")
# print unicode(self.s.recv(1024),"utf-8")
print self.s.recv(1024)
threading.Thread(target=clientInput,args=(self,)).start()
while 1:
try:
readable,writeable,exceptional = select.select([self.s],[],[])
for sock in readable:
if sock == self.s:
data = sock.recv(1024)
if not data:
print unicode("Server is closed","utf-8")
sys.exit(0)
sys.stdout.write(data)
sys.stdout.flush()
else:
data = sys.stdin.readline()
if data.startswith("QUIT"):
print unicode("Client is closed","utf-8")
sys.exit(0)
self.s.sendall(data)
except KeyboardInterrupt:
print unicode("Client is closed","utf-8")
break
except Exception,e:
print unicode("failed"+e.message,"utf-8")
def clientInput(s):
while 1:
data = sys.stdin.readline()
s.s.sendall(data)
if data.lower().startswith("quit"):
s.s.shutdown(socket.SHUT_RDWR)
s.s.close()
break
if __name__ == '__main__':
typename = raw_input("Please chose the type :server|client >>> ")
if typename.lower().startswith("server"):
host = raw_input("Please input your host : IP Adress >>> ")
port = int(raw_input("input your port :1024-65535 >>> "))
server = ChatroomServer(host,port)
server.run()
elif typename.lower().startswith("client"):
host = raw_input("Please input your host : IP Adress >>> ")
port = int(raw_input("Please input your port :1024-65535 >>> "))
name = raw_input("Please input your name >>> ")
client = ChatroomClient(host,port,name)
client.run()