Respond to Multiple Pings #wy1

def main():
    # You can use print statements as follows for debugging, they'll be visible when running tests.
    print("Logs from your program will appear here!")

    server_socket = socket.create_server(("localhost", 6379), reuse_port=True)
    
    server_socket.listen(6379) # listen on the port

    while True:
        client_socket,address = server_socket.accept() # wait for client request and accept the connection
        
        while True:
            data_input = client_socket.recv(2048) # read the data from the client

            if not data_input:
                break

            client_socket.send(b"+PONG\r\n") # reply to the client with pong (hardcoded for now)
            
        if not data_input:
            break

    
    client_socket.close()

I submitted this code and it works but it looks like utter garbage and I know that im writing some redudant logic somewhere but im unable to figure out what it is because I dont know enough yet is this really the right way to read multiple messages from one client socket?

@shreyasganesh0 The inner while True is absolutely correct as you need to read multiple commands from the same connection (socket).

However, the outer while True is probably unneccesary.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.