forked from AntonY4ZerO/2_threaded_server_PI20-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
32 lines (27 loc) · 818 Bytes
/
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
import socket
import threading
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 9091))
sock.listen(5)
while True:
threads = [threading.Thread(target=connect, args=[sock]) for _ in range(5)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
def connect(sock):
while True:
conn, addr = sock.accept()
print(f'Новый пользователь: {addr}')
if conn is not None:
new_client(conn)
def new_client(conn):
name = conn.recv(1024)
name = name.decode()
conn.send('Подключение установлено.'.encode())
output(conn)
def output(conn):
while True:
data = conn.recv(1024)
print(data.decode())
if __name__ == '__main__':
main()