|
@@ -243,7 +243,8 @@ class Server(object):
|
|
# set to true when the server shuts down, for instance after a
|
|
# set to true when the server shuts down, for instance after a
|
|
# fatal exception
|
|
# fatal exception
|
|
exit_event = False
|
|
exit_event = False
|
|
- def __init__(self,
|
|
|
|
|
|
+ def __init__(
|
|
|
|
+ self,
|
|
host,
|
|
host,
|
|
port,
|
|
port,
|
|
handler_class,
|
|
handler_class,
|
|
@@ -423,62 +424,50 @@ class Server(object):
|
|
|
|
|
|
class Client(threading.Thread):
|
|
class Client(threading.Thread):
|
|
"""docstring for Client"""
|
|
"""docstring for Client"""
|
|
- is_connecting = False
|
|
|
|
- is_connected = False
|
|
|
|
status = "uninitialized"
|
|
status = "uninitialized"
|
|
- def __init__(self,
|
|
|
|
|
|
+ exit_event = False
|
|
|
|
+ def __init__(
|
|
|
|
+ self,
|
|
host,
|
|
host,
|
|
port=None,
|
|
port=None,
|
|
af_family="AF_INET",
|
|
af_family="AF_INET",
|
|
handle_data_func=None,
|
|
handle_data_func=None,
|
|
error_handler=None,
|
|
error_handler=None,
|
|
block_size=1024,
|
|
block_size=1024,
|
|
- ):
|
|
|
|
- self.super_class = super(Client, self)
|
|
|
|
- self.super_class.__init__()
|
|
|
|
|
|
+ ):
|
|
|
|
+ super()__init__()
|
|
self.name = "Client"
|
|
self.name = "Client"
|
|
- self.exit_event = False
|
|
|
|
self.host = host
|
|
self.host = host
|
|
self.port = port
|
|
self.port = port
|
|
self.af_family = af_family
|
|
self.af_family = af_family
|
|
self.block_size = block_size
|
|
self.block_size = block_size
|
|
self.handle_data_func = handle_data_func
|
|
self.handle_data_func = handle_data_func
|
|
- self.is_connected = False
|
|
|
|
self.error_handler = error_handler
|
|
self.error_handler = error_handler
|
|
- # self.socket = self.make_socket()
|
|
|
|
self.socket = None
|
|
self.socket = None
|
|
self.status = "disconnected"
|
|
self.status = "disconnected"
|
|
|
|
+
|
|
def connect(self):
|
|
def connect(self):
|
|
self.status = "connecting"
|
|
self.status = "connecting"
|
|
self.socket = self.make_socket()
|
|
self.socket = self.make_socket()
|
|
- lclient.info(
|
|
|
|
- "connecting to socket '{}' of type {}".format(
|
|
|
|
- self.host,
|
|
|
|
- self.af_family
|
|
|
|
- )
|
|
|
|
- )
|
|
|
|
|
|
+ lclient.info("connecting to socket '{}' of type {}"
|
|
|
|
+ .format(self.host,self.af_family))
|
|
try:
|
|
try:
|
|
if self.af_family == "AF_INET":
|
|
if self.af_family == "AF_INET":
|
|
self.socket.connect((self.host, self.port))
|
|
self.socket.connect((self.host, self.port))
|
|
elif self.af_family == "AF_UNIX":
|
|
elif self.af_family == "AF_UNIX":
|
|
- if os.path.exists(self.host):
|
|
|
|
- self.socket.connect(self.host)
|
|
|
|
- else:
|
|
|
|
- lclient.warn("File not found. Aborting.")
|
|
|
|
- return
|
|
|
|
- self.is_connected = True
|
|
|
|
|
|
+ self.socket.connect(self.host)
|
|
self.status = "connected"
|
|
self.status = "connected"
|
|
lclient.info("connected")
|
|
lclient.info("connected")
|
|
return True
|
|
return True
|
|
except Exception as e:
|
|
except Exception as e:
|
|
lclient.debug(e, exc_info=True)
|
|
lclient.debug(e, exc_info=True)
|
|
if type(e) is ConnectionRefusedError:
|
|
if type(e) is ConnectionRefusedError:
|
|
- lclient.info("failed to connect to socket '{}'".format(self.host))
|
|
|
|
|
|
+ lclient.info(
|
|
|
|
+ "failed to connect to socket '{}'".format(self.host))
|
|
self.disconnect()
|
|
self.disconnect()
|
|
return False
|
|
return False
|
|
def disconnect(self):
|
|
def disconnect(self):
|
|
lclient.info("disconnecting from socket '{}'".format(self.host))
|
|
lclient.info("disconnecting from socket '{}'".format(self.host))
|
|
- self.is_connected = False
|
|
|
|
self.status = "disconnected"
|
|
self.status = "disconnected"
|
|
if self.socket:
|
|
if self.socket:
|
|
try:
|
|
try:
|
|
@@ -530,7 +519,6 @@ class Client(threading.Thread):
|
|
1
|
|
1
|
|
)
|
|
)
|
|
if self.socket in exc_confirmed:
|
|
if self.socket in exc_confirmed:
|
|
- self.is_connected = False
|
|
|
|
lclient.warning("socket is expected to corrupt, exiting")
|
|
lclient.warning("socket is expected to corrupt, exiting")
|
|
self.disconnect()
|
|
self.disconnect()
|
|
# self.stop()
|
|
# self.stop()
|
|
@@ -554,7 +542,6 @@ class Client(threading.Thread):
|
|
except Exception as e:
|
|
except Exception as e:
|
|
lclient.error(e, exc_info=True)
|
|
lclient.error(e, exc_info=True)
|
|
if type(e) is OSError:
|
|
if type(e) is OSError:
|
|
- self.is_connected = False
|
|
|
|
lclient.warn("connection broken, exiting")
|
|
lclient.warn("connection broken, exiting")
|
|
self.disconnect()
|
|
self.disconnect()
|
|
# self.stop()
|
|
# self.stop()
|
|
@@ -579,7 +566,6 @@ class Client(threading.Thread):
|
|
lcchat.info("Client: "+msg)
|
|
lcchat.info("Client: "+msg)
|
|
self.socket.send(msg_encoded)
|
|
self.socket.send(msg_encoded)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- self.is_connected = False
|
|
|
|
lclient.error(e, exc_info=True)
|
|
lclient.error(e, exc_info=True)
|
|
self.status = "shutdown"
|
|
self.status = "shutdown"
|
|
def setup(self):
|
|
def setup(self):
|
|
@@ -598,27 +584,24 @@ class Client(threading.Thread):
|
|
|
|
|
|
class AsyncClient(object):
|
|
class AsyncClient(object):
|
|
"""docstring for Client"""
|
|
"""docstring for Client"""
|
|
- is_connecting = False
|
|
|
|
- is_connected = False
|
|
|
|
|
|
+ block_size = 1024
|
|
status = "uninitialized"
|
|
status = "uninitialized"
|
|
- def __init__(self,
|
|
|
|
|
|
+ exit_event = False
|
|
|
|
+ def __init__(
|
|
|
|
+ self,
|
|
host,
|
|
host,
|
|
port=None,
|
|
port=None,
|
|
af_family="AF_INET",
|
|
af_family="AF_INET",
|
|
handle_data_func=None,
|
|
handle_data_func=None,
|
|
error_handler=None,
|
|
error_handler=None,
|
|
block_size=1024,
|
|
block_size=1024,
|
|
- ):
|
|
|
|
- self.super_class = super(AsyncClient, self)
|
|
|
|
- self.super_class.__init__()
|
|
|
|
- self.name = "Client"
|
|
|
|
- self.exit_event = False
|
|
|
|
|
|
+ ):
|
|
|
|
+ super().__init__()
|
|
self.host = host
|
|
self.host = host
|
|
self.port = port
|
|
self.port = port
|
|
self.af_family = af_family
|
|
self.af_family = af_family
|
|
self.block_size = block_size
|
|
self.block_size = block_size
|
|
self.handle_data_func = handle_data_func
|
|
self.handle_data_func = handle_data_func
|
|
- self.is_connected = False
|
|
|
|
self.error_handler = error_handler
|
|
self.error_handler = error_handler
|
|
self.socket = None
|
|
self.socket = None
|
|
self.status = "disconnected"
|
|
self.status = "disconnected"
|