-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalf_duplex_chat_server.py
More file actions
32 lines (28 loc) · 1.13 KB
/
half_duplex_chat_server.py
File metadata and controls
32 lines (28 loc) · 1.13 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
#!/usr/bin/env python3
"""TCP Half-Duplex Chat Server Client"""
from socket import *
from time import ctime
HOST = ""
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
INDT = 25 # Indentation for messages alignment
with socket(AF_INET, SOCK_STREAM) as chat_server:
chat_server.bind(ADDR)
chat_server.listen(1)
while True:
print("Waiting for connections...")
conn, addr = chat_server.accept()
with conn:
print("[{0}]".format(ctime()), "Connection is made with", addr)
while True:
client_data = conn.recv(BUFSIZ)
if not client_data: break
print("{0:<{indt}}{1}".format("Client message:",
client_data.decode(),
indt=INDT))
server_data = input("{0:<{indt}}".format("Enter server message:",
indt=INDT))
if not server_data: break
conn.sendall(bytes(server_data, 'utf-8'))
print("[{0}]".format(ctime()), "Connection is closed with", addr)