-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSocketChatroom_1_0.py
122 lines (111 loc) · 3.66 KB
/
SocketChatroom_1_0.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
#coding=utf-8
import sys
import socket
import select
import argparse
def runserver(host,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(10)
print unicode("Server is running ... ","utf-8")
inputs = [s]
outputs = []
clients = {}
while True:
try:
readable,writeable,exceptional = select.select(inputs,outputs,[])
for sock in readable:
if sock == 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")
clients[clientsock] = (clientname,clientaddr,clientsock)
inputs.append(clientsock)
for output in outputs:
output.sendall("Welcome " + clientname + " Come In \n")
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 outputs:
output.sendall("Server : " + message)
else:
data = sock.recv(1024)
if data:
if data.startswith("SECRECT"):
print unicode("SECRECT " + clients[sock][0] + " : " + data,"utf-8")
output = data.split(" ")[1]
message = data.split(" ")[2]
for client in clients.values():
if client[0] == output:
client[2].sendall("SECRECT " + clients[sock][0] + " : " + message)
else:
print unicode(clients[sock][0] + " : " + data,"utf-8")
for output in outputs:
if output != sock:
output.sendall(clients[sock][0] + " : " + data)
else:
name = clients[sock][0]
print unicode(name+" leaved ","utf-8")
for output in outputs:
output.sendall(name+" leaved \n")
inputs.remove(sock)
outputs.remove(sock)
del clients[sock]
except KeyboardInterrupt:
print unicode("Server is close ... ","utf-8")
break
def runclient(host,port,name=None):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.connect((host,port))
if name!=None:
s.sendall("NAME:"+name)
else:
s.sendall("NAME:")
print unicode(s.recv(1024),"utf-8")
while True:
try:
readable,writeable,exceptional = select.select([s],[],[])
for sock in readable:
if sock == 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)
s.sendall(data)
except KeyboardInterrupt:
print unicode("Client is closed","utf-8")
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="socket chatroom")
parser.add_argument("--type",help="chose the type :server|client",action="store",default="client",dest="type")
parser.add_argument("--host",help="input your host : IP Adress",action="store",default="127.0.0.1",dest="host")
parser.add_argument("--port",help="input your port :1024-65535",action="store",default=8888,type=int,dest="port")
parser.add_argument("--name",help="input your name ",action="store",default=None,dest="name")
args = parser.parse_args()
chattype = args.type
host = args.host
port = args.port
name = args.name
if chattype.startswith("server"):
runserver(host,port)
elif chattype.startswith("client"):
runclient(host,port,name)
else:
print unicode("your input is wrong","utf-8")