Commit a4b605ae authored by Guido Trotter's avatar Guido Trotter
Browse files


This is a new asyncore server which handles listening stream sockets by
calling a non-implemented function for each connection it accepts. It's
the stream-oriented cousing of the AsyncUDPSocket.
Signed-off-by: default avatarGuido Trotter <>
Reviewed-by: default avatarMichael Hanselmann <>
parent db147305
......@@ -91,6 +91,75 @@ class GanetiBaseAsyncoreDispatcher(asyncore.dispatcher):
return False
def FormatAddress(family, address):
"""Format a client's address
@type family: integer
@param family: socket family (one of socket.AF_*)
@type address: family specific (usually tuple)
@param address: address, as reported by this class
if family == socket.AF_INET and len(address) == 2:
return "%s:%d" % address
elif family == socket.AF_UNIX and len(address) == 3:
return "pid=%s, uid=%s, gid=%s" % address
return str(address)
class AsyncStreamServer(GanetiBaseAsyncoreDispatcher):
"""A stream server to use with asyncore.
Each request is accepted, and then dispatched to a separate asyncore
dispatcher to handle.
def __init__(self, family, address):
"""Constructor for AsyncUnixStreamSocket
@type family: integer
@param family: socket family (one of socket.AF_*)
@type address: address family dependent
@param address: address to bind the socket to
GanetiBaseAsyncoreDispatcher.__init__(self) = family
self.create_socket(, socket.SOCK_STREAM)
# this method is overriding an asyncore.dispatcher method
def handle_accept(self):
"""Accept a new client connection.
Creates a new instance of the handler class, which will use asyncore to
serve the client.
accept_result = utils.IgnoreSignals(self.accept)
if accept_result is not None:
connected_socket, client_address = accept_result
if == socket.AF_UNIX:
# override the client address, as for unix sockets nothing meaningful
# is passed in from accept anyway
client_address = utils.GetSocketCredentials(connected_socket)"Accepted connection from %s",
FormatAddress(, client_address))
self.handle_connection(connected_socket, client_address)
def handle_connection(self, connected_socket, client_address):
"""Handle an already accepted connection.
raise NotImplementedError
class AsyncUDPSocket(GanetiBaseAsyncoreDispatcher):
"""An improved asyncore udp socket.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment