Skip to content

Instantly share code, notes, and snippets.

@ahmedfgad
Created June 23, 2020 19:41
Show Gist options
  • Save ahmedfgad/b67f30cdc929a0f16a095e5af6aab80b to your computer and use it in GitHub Desktop.
Save ahmedfgad/b67f30cdc929a0f16a095e5af6aab80b to your computer and use it in GitHub Desktop.
while True:
accept_timeout = 10
soc.settimeout(accept_timeout)
try:
connection, address = soc.accept()
print("Connected to a client: {client_info}.".format(client_info=address))
received_data = b''
while True:
recv_start_time = time.time()
time_struct = time.gmtime()
date_time = "Waiting to Receive Data Starting from {day}/{month}/{year} {hour}:{minute}:{second} GMT".format(year=time_struct.tm_year, month=time_struct.tm_mon, day=time_struct.tm_mday, hour=time_struct.tm_hour, minute=time_struct.tm_min, second=time_struct.tm_sec)
print(date_time)
recv_timeout = 5
received_data, status = recv(connection, 8, recv_timeout)
print("Received data from the client: {received_data}".format(received_data=received_data))
if status == 0:
connection.close()
print("Connection Closed either due to inactivity for {recv_timeout} seconds or due to an error.".format(recv_timeout=recv_timeout), end="\n\n")
break
msg = "Reply from the server."
msg = pickle.dumps(msg)
connection.sendall(msg)
print("Server sent a message to the client.")
except socket.timeout:
soc.close()
print("A socket.timeout exception occurred because the server did not receive any connection for {accept_timeout} seconds.".format(accept_timeout=accept_timeout))
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment