3

I have simple function that act like a client. It sents an event message and wait for an ack(I removed that part for simplicity). After that it'll get receive more messages:

def send_data(message):
    """Connect to server and get requests"""
    sock = socket.socket()
    host = 'localhost'
    port = 8000
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host,port))

    try:
        if message:
            sock.sendall(message)

        # Look for the response
        has_request = False
        while not has_request:
            data = sock.recv(1024)

            #First I get ACK, code not listed here...
            #Next iteration, I got my request message
            has_request = True

    finally:
        print >> sys.stderr, 'Closing socket'
        sock.close() 

    return data

I call this in the __main__ statement:

if __name__ == '__main__':
    server_data = send_data(event)

    if server_data:
        parse_request(server_data)

However I need to prevent the socket to be closed automatically. Because I have to sent another messagea after I parsed the request. I removed the sock.close() but the socket still get closed. Is this because I'm returing the data and I'm out of the functions scope?

I just want to connect, and then receive and sent messages without any problems, at any time in my program.

1 Answer 1

2

When you go outside the send_data method, your socket sock will no longer be referenced by anybody, so the garbage collector will do his job and delete the object, that will consequently close the socket (even without the close() call).

If you return the socket object with your data and get it in your main part, the socket will be still referenced and not closed.

def create_socket():
    sock = socket.socket()
    host = 'localhost'
    port = 8000
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host,port))
    return sock

def send_data(message, sock):
    """Get requests"""
    if message:
        sock.sendall(message)

    # Look for the response
    has_request = False
    while not has_request:
        data = sock.recv(1024)

        #First I get ACK, code not listed here...
        #Next iteration, I got my request message
        has_request = True
    return data

if __name__ == '__main__':
    server_sock = create_socket()
    server_data = send_data(event, server_sock)

    if server_data:
        parse_request(server_data, server_sock)

    #no more use of server_socket ? 
    server_sock.close()
Sign up to request clarification or add additional context in comments.

3 Comments

Then it is a better approach to return the socket and to the socket.recv and socket.sendall in other function?
@FatihArslan : maybe the better approach would be to put the socket management in a object/function that will create the socket and then give it to who need this socket :)
I creat a new sockeet Class to make it much more easier. Thanks for the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.