-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
26 lines (21 loc) · 766 Bytes
/
server.py
File metadata and controls
26 lines (21 loc) · 766 Bytes
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
import socket
BIND_PORT = 9999
def start_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', BIND_PORT))
s.listen()
print('TCP server bind on 9999...')
conn, addr = s.accept()
print(f'accepted {addr}')
try:
while True:
data = conn.recv(1024)
if data:
print(f'from client ({addr[0]}.{addr[1]}): {data.decode("utf-8")}')
else:
break
conn.send(f'Hello, client ({addr[0]})! This is server ({conn.getsockname()[0]})'.encode('utf-8'))
except KeyboardInterrupt or Exception:
s.shutdown(socket.SHUT_RDWR)
if __name__ == '__main__':
start_server()