Home > database >  How to accept all data from connection in socketserver python?
How to accept all data from connection in socketserver python?

Time:12-02

How to receive all data from a connection in socketserver so that it the connection does not hang on the client side

class ConnectionHandler(BaseRequestHandler):
    def handle(self):
        data = b''
        while 1:
            tmp =  self.request.recv(1024)
            if not tmp:
                break
            data  = tmp 
        print (data.decode())

on the client side I am using

    char text[] = "Hello world\n";
    SSL_write(ssl, text, sizeof(text));

    char tmp[20];
    int received = SSL_read (ssl, tmp, 20);
    printf("Server replied: [%s]\n", tmp);

but this causes the connection not to close and the client hangs, I am sure this is the case since replacing the while loop with self.request.recv(1024) receives the client message and outputs it but what if i don't know the message size of the client

CodePudding user response:

In order to receive all data from a connection in socketserver, you can use the makefile method of the socket object. This method returns a file-like object that can be used to read data from the connection. Here is an example of how you could use this method to receive all data from the connection:

class ConnectionHandler(BaseRequestHandler):

def handle(self):
    # Use the makefile method to get a file-like object for the connection
    file_like_obj = self.request.makefile('rb')
    # Read all data from the file-like object
    data = file_like_obj.read()
    print(data.decode())

This approach allows you to read all data from the connection without having to manually manage the receive buffer. Additionally, since the makefile method returns a file-like object, you can use the familiar file operations like read, readline, and readlines to read data from the connection.

However, keep in mind that using the makefile method to read data from the connection will consume the data from the receive buffer. This means that if you also want to use the recv method to read data from the connection, you will need to call the recv method before calling the makefile method.

In your specific example, it looks like you are using SSL to encrypt the data that is sent over the connection. In this case, you should use the SSL_makefile method instead of the makefile method in order to get a file-like object for the connection. This method is similar to the makefile method, but it is used for SSL connections. Here is an example of how you could use the SSL_makefile method to receive all data from an SSL connection:

class ConnectionHandler(BaseRequestHandler):

def handle(self):
    # Use the SSL_makefile method to get a file-like object for the SSL connection
    file_like_obj = self.request.SSL_makefile('rb')
    # Read all data from the file-like object
    data = file_like_obj.read()
    print(data.decode())

I hope this helps. Let me know if you have any questions.

CodePudding user response:

Using self.request.recv() without an actual value on how many bytes to read seems to obtain the whole buffer from the client

  • Related