Home > Software engineering >  How to receive entire data comming from server in python socket
How to receive entire data comming from server in python socket

Time:08-29

I am Using Python socket lib, i want to receive the entire data coming from server i have tried using loops but it stucking after reading the data.

    def readReply(self):
        recv_data = b''
        while True:
            data = self.client_socket.recv(1024)
            if data:
                recv_data  = data
            else:
                break
        return recv_data

CodePudding user response:

What I mean about the timeout is this:

def readReply(self):
    recv_data = b''

    # This will block the socket for 0.1s
    #
    # If you have a poor internet connection, you
    # may need to increase this value to a higher
    # one.
    self.client_socket.settimeout(0.1)
    while True:
        try:
            data = self.client_socket.recv(1024)
        except:
            break
        else:
            recv_data  = data

    return recv_data

Once the timeout is reached, the while-loop will break. Note: if you don't want to freeze the main thread while waiting for the incoming data (in case you're using a GUI), you should use multithreading.

  • Related