hio.core.tcp.serving
hio.core.tcp.serving Module
Accepter listens and accepts incoming TCP socket connections Server is subclass of Acceptor Server creates Remoters Remoter is accepted incoming socket connection
ServerTls is subclass of Server RemoterTls is subclass of Remoter
Module Contents
- hio.core.tcp.serving.openServer(cls=None, **kwa)
Wrapper to create and open TCP Server instances When used in with statement block, calls .close() on exit of with block
- Parameters:
instance (cls is Class instance of subclass)
Usage:
with openServer() as server0: server0. with openServer(cls=ServerTls) as server0: server0.
- class hio.core.tcp.serving.Acceptor(ha=None, bs=8096, bl=128, **kwa)
Bases:
hio.base.tyming.TymeeAcceptor Base Class for Server. Nonblocking TCP Socket Acceptor Class. Listen socket for incoming TCP connections
See tyming.Tymee for inherited attributes, properties, and methods
- .bs is buffer size
- .ss is server listen socket for incoming accept requests
- .axes is deque of accepte connection duples
- Type:
ca, cs
- .opened is boolean, True if listen socket .ss opened. False otherwise
- actualBufSizes()
Returns duple of the the actual socket send and receive buffer size (send, receive)
- open()
Opens binds listen socket in non blocking mode.
- if socket not closed properly, binding socket gets error
OSError: (48, ‘Address already in use’)
- reopen(**kwa)
Idempotently opens listen socket
- close()
Closes listen socket.
- accept()
Accept new connection nonblocking Returns duple (cs, ca) of connected socket and connected host address Otherwise if no new connection returns (None, None)
- serviceAccepts()
Service any accept requests Adds to .cxes dict key by ca
- class hio.core.tcp.serving.Server(ha=None, host='', port=56000, tymeout=None, wl=None, **kwa)
Bases:
AcceptorNonblocking TCP Socket Server Class. Listen socket for incoming TCP connections that generates Remoter sockets for accepted connections
See tyming.Tymee for inherited attributes, properties, and methods
- Inherited Attributes:
ha (tuple[str, int]): (host, port) duple; host = “” or “0.0.0.0” means listen on all interfaces eha (tuple[str, int]): normalized external-facing (host, port) for TLS context bs (int): buffer size ss (socket.socket | None): server listen socket for incoming accept requests axes (deque): accepted connection duples (ca, cs) opened (bool): True if listen socket ss is open; False otherwise
- tymeout
timeout in seconds for connection refresh
- Type:
float
- ixes
incoming connections indexed by remote (host, port) duple
- Type:
dict
- wind(tymth)
Inject new tymist.tymth as new ._tymth. Changes tymist.tyme base. Updates winds .tymer .tymth
- serviceAxes()
Service axes
For each newly accepted connection in .axes create Remoter and add to .ixes keyed by ca
- serviceConnects()
Service connects is method name to be used
- shutdownIx(ca, how=socket.SHUT_RDWR)
Shutdown remoter given by connection address ca
- shutdownSendIx(ca)
Shutdown send on remoter given by connection address ca
- shutdownReceiveIx(ca)
Shutdown send on remoter given by connection address ca
- closeIx(ca)
Shutdown and close remoter given by connection address ca
- closeAllIx()
Shutdown and close all remoter connections
- close()
Close all sockets
- removeIx(ca, close=True)
Remove remoter given by connection address ca
- serviceReceivesIx(ca)
Service receives for remoter by connection address ca
- serviceReceivesAllIx()
Service receives for all remoters in .ixes
- transmitIx(data, ca)
Queue data onto .txbs for remoter given by connection address ca
- serviceSendsAllIx()
Service transmits for all remoters in .ixes
- service()
Service connects and service receives and sends for all ix.
- hio.core.tcp.serving.initServerContext(context=None, version=None, certify=None, keypath=None, certpath=None, cafilepath=None)
Initialize and return context for TLS Server IF context is None THEN create a context
IF version is None THEN create context using ssl library default ELSE create context with version
If certify is not None then use certify value provided Otherwise use default
context is the tls/ssl context object; if None use default. version is ssl protocol version; if None use default. certify is cert requirement; if None use default (ssl.CERT_NONE, ssl.CERT_OPTIONAL, ssl.CERT_REQUIRED). keypath is local server PKI private key file path; if given apply to context. certpath is local server PKI public cert file path; if given apply to context. cafilepath is cert authority file path to verify client cert; if given apply.
- class hio.core.tcp.serving.ServerTls(context=None, version=None, certify=None, keypath=None, certpath=None, cafilepath=None, **kwa)
Bases:
ServerServer with Nonblocking TLS/SSL support Nonblocking TCP Socket Server Class. Listen socket for incoming TCP connections RemoterTLS sockets for accepted connections
See tyming.Tymee for inherited attributes, properties, and methods
- Inherited Attributes:
ha (tuple[str, int]): (host, port) duple; host = “” or “0.0.0.0” means listen on all interfaces eha (tuple[str, int]): normalized external-facing (host, port) for TLS context bs (int): buffer size ss (socket.socket | None): server listen socket for incoming accept requests axes (deque): accepted connection duples (ca, cs) opened (bool): True if listen socket ss is open; False otherwise timeout (float): timeout in seconds for connection refresh wl (WireLog | None): WireLog instance if any ixes (dict): incoming connections indexed by remote (host, port) duple
- context
TLS context instance
- Type:
ssl.SSLContext | None
- version
TLS version
- Type:
int | None
- certify
True to require client cert verification
- Type:
bool | None
- keypath
path to key file
- Type:
str | None
- certpath
path to cert file
- Type:
str | None
- cafilepath
path to CA file
- Type:
str | None
- serviceAxes()
Service accepteds
For each new accepted connection create RemoterTLS and add to .cxes Not Handshaked
- serviceCxes()
Service handshakes for every remoter in .cxes If successful move to .ixes
- serviceConnects()
Service accept and handshake attempts For each successful handshake add to .ixes.
- class hio.core.tcp.serving.Remoter(ha, ca, cs, tymeout=None, refreshable=True, bs=8096, wl=None, **kwa)
Bases:
hio.base.tyming.TymeeClass to service an incoming nonblocking TCP connection from a remote client. Should only be used by an Acceptor subclass such as Server
- wind(tymth)
Inject new tymist.tymth as new ._tymth. Changes tymist.tyme base. Updates winds .tymer .tymth
- shutdown(how=socket.SHUT_RDWR)
Shutdown connected socket .cs
- shutdownSend()
Shutdown send on connected socket .cs
- shutdownReceive()
Shutdown receive on connected socket .cs
- close()
Shutdown and close connected socket .cs
- refresh()
Restart tymer
- receive()
Perform non blocking receive on connected socket .cs
If no data then returns None If connection closed then returns ‘’ Otherwise returns data
data is string in python2 and bytes in python3
- serviceReceives()
Service receives until no more
- serviceReceiveOnce()
Retrieve from server only one reception
- clearRxbs()
Clear .rxbs
- send(data)
Perform non blocking send on connected socket .cs. Return number of bytes sent
data is string in python2 and bytes in python3
- tx(data)
Queue data onto .txbs
- serviceSends()
Service transmits For each tx if all bytes sent then keep sending until partial send or no more to send If partial send reattach and return
- class hio.core.tcp.serving.RemoterTls(context=None, version=None, certify=None, keypath=None, certpath=None, cafilepath=None, **kwa)
Bases:
RemoterClass to service an incoming nonblocking TCP/TLS connection from a remote client. Should only be used from Acceptor subclass Provides nonblocking TLS/SSL support
- connected
True means TLS handshake completed False otherwise
- Type:
bool
- aborted
True means client aborted TLS handshake False otherwise
- Type:
bool
- close()
Shutdown and close connected socket .cs
- wrap()
Wrap socket .cs in ssl context
- handshake()
Attempt nonblocking ssl handshake to .ha Sets .connected when complete successfully Sets .aborted when client terminates prematurely Keep trying while not connected and not aborted
- receive()
Perform non blocking receive on connected socket .cs
If no data then returns None If connection closed then returns ‘’ Otherwise returns data
data is string in python2 and bytes in python3
- send(data)
Perform non blocking send on connected socket .cs. Return number of bytes sent
data is string in python2 and bytes in python3
- class hio.core.tcp.serving.ServerDoer(server, **kwa)
Bases:
hio.base.doing.DoerBasic TCP Server Doer
See Doer for inherited attributes, properties, and methods.
- .server is TCP Server instance
Properties:
- wind(tymth)
Inject new tymist.tymth as new ._tymth. Changes tymist.tyme base. Updates winds .tymer .tymth
- enter(*, temp=None)
Do ‘enter’ context actions. Override in subclass. Not a generator method. Set up resources. Comparable to context manager enter.
- Parameters:
temp (bool | None) – True means use temporary file resources if any None means ignore parameter value. Use self.temp
Inject temp or self.temp into file resources here if any
Doist or DoDoer winds its doers on enter
- recur(tyme)
Do ‘recur’ context actions. Override in subclass. Regular method that perform repetitive actions once per invocation. Assumes resource setup in .enter() and resource takedown in .exit() (see ReDoer below for example of .recur that is a generator method)
Returns completion state of recurrence actions: True means done, False means continue.
Doist provides the time value.
.recur maybe implemented by a subclass either as a non-generator method or a generator method. This stub here is as a non-generator method. The base class .do detects which type:
If non-generator .do method runs .recur method once per iteration until .recur returns (True) If generator .do method runs .recur with (yield from) until .recur returns (see ReDoer for example of generator .recur)
- exit()
Do ‘exit’ context actions. Override in subclass. Not a generator method. Clean up resources. Comparable to context manager exit. Called by finally after normal return, close, or abort. After .exit() do returns resulting in StopIteration.
- class hio.core.tcp.serving.EchoServerDoer(server, **kwa)
Bases:
ServerDoerEcho TCP Server Just echoes back to client whatever it receives from client
See Doer for inherited attributes, properties, and methods.
- Inherited Attributes:
.server is TCP Server instance
- recur(tyme)
Do ‘recur’ context actions. Override in subclass. Regular method that perform repetitive actions once per invocation. Assumes resource setup in .enter() and resource takedown in .exit() (see ReDoer below for example of .recur that is a generator method)
Returns completion state of recurrence actions: True means done, False means continue.
Doist provides the time value.
.recur maybe implemented by a subclass either as a non-generator method or a generator method. This stub here is as a non-generator method. The base class .do detects which type:
If non-generator .do method runs .recur method once per iteration until .recur returns (True) If generator .do method runs .recur with (yield from) until .recur returns (see ReDoer for example of generator .recur)
- exit()
Do ‘exit’ context actions. Override in subclass. Not a generator method. Clean up resources. Comparable to context manager exit. Called by finally after normal return, close, or abort. After .exit() do returns resulting in StopIteration.