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.
When you go outside the
send_datamethod, your socketsockwill 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 theclose()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.