-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.py
More file actions
40 lines (31 loc) · 1.15 KB
/
Copy pathecho_server.py
File metadata and controls
40 lines (31 loc) · 1.15 KB
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
"""An echo server that has a server thread and a client thread. ONLY 5 CONNECTIONS."""
import threading
import socket
def server() -> None:
"""A server thread that has a server thread"""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 8007
host = 'localhost'
server_socket.bind((host, port))
server_socket.listen(1)
for _ in range(5):
conn, addr = server_socket.accept()
data = conn.recv(100000000)
print('connected: ', addr, data.decode('utf-8'))
conn.send(data)
conn.close()
def client() -> None:
"""A client thread that has a client thread"""
for _ in range(5):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 8007
host = 'localhost'
client_socket.connect((host, port))
client_socket.send(input('> ').encode('utf-8'))
data = client_socket.recv(100000000)
print('received', data.decode('utf-8'), len(data), 'bytes')
client_socket.close()
server_thread = threading.Thread(target=server)
client_thread = threading.Thread(target=client)
server_thread.start()
client_thread.start()