I am creating a server with Python 3 socketserver and I have an issue while trying to restart the connection. I want to be able to close it, read configuration from a file and start again, however, I am running into the Address already in use
OSError. I thought that using server_close()
method it would clear itself (as specified in the doc) so that I will be able to start again with it, but I'm not.
Here is my code:
class NetworkCommunicationService(Services):
def __init__(self):
self.__connection__ = None
self.__setup__()
def __setup__(self):
host, port = get_setup()
self.__connection__ = ConnectionHandler.setup_connection_handler(host, port)
def service_entry_point(self):
try:
self.__connect__()
except FileNotFoundError:
Output.config_error()
finally:
self.__connection__.shutdown()
def __connect__(self):
self.__connection__.serve_forever()
def terminate(self):
self.__connection__.shutdown()
self.__connection__.server_close()
def reset(self):
self.terminate()
self.__setup__()
self.service_entry_point()
The handle()
method called in serve_forever()
is taken from some website I can't remember right now. I only added the close()
at the end hoping it would help.
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024)
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
self.request.close()
What shall I do to be able to reset the connection?