-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
53 lines (39 loc) · 1.12 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
50
51
52
# EE450 programming assignment 3
# Jiayi Zhang 3032914272
import socket
import locale
import select
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
locale.setlocale(locale.LC_ALL,'en_US')
buffer_size = 1000
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
sock.bind((TCP_IP, TCP_PORT))
print("Wait for connection...")
sock.listen(5)
ID = 1
total_bytes = []
clients = []
inputs = [sock]
while True:
[rlist, wlist, xlist] = select.select(inputs, [], [])
for r in rlist:
if r is sock:
conn, addr = sock.accept()
inputs.append(conn)
clients.append(conn)
print ('\nConnected to address as client %d: %s: %d\n' % (ID, addr[0],addr[1]))
ID += 1
total_bytes.append(0)
else:
client_id = clients.index(r)
data = r.recv(buffer_size)
# if not data:
# break
if data:
bytes = len(data)
total_bytes[client_id] += bytes
bytes = locale.format('%d', bytes, grouping = True)
total = locale.format('%d', total_bytes[client_id], grouping = True)
print("Received %s bytes from Client %d. Total: %s." % (bytes, client_id + 1, total))