This program cannot exit because the accept function blocks.
What can be done so that the program exits after a timeout even if the accept function is still running?
# taken from https://realpython.com/python-sockets/#echo-server
import socket
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
def connect ():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
print("listening")
s.listen()
print("accepting")
conn, addr = s.accept() # If these lines are
with conn: # commented, the program
print(f"Connected by {addr}") # exits normally.
while True: # This is why I think
data = conn.recv(1024) # that the *accept* function
if not data: # is the problem
break #
conn.sendall(data) #
def main():
connect()
if __name__ == "__main__":
main()