pynetdicom.transport.AssociationServer¶
- class pynetdicom.transport.AssociationServer(ae: ApplicationEntity, address: tuple[str, int], ae_title: str, contexts: list[PresentationContext], ssl_context: ssl.SSLContext | None = None, evt_handlers: list[tuple[NotificationEvent | InterventionEvent, Callable] | tuple[NotificationEvent | InterventionEvent, Callable, list[Any]]] | None = None, request_handler: Callable[[...], BaseRequestHandler] | None = None)[source]¶
An Association server implementation.
Added in version 1.2.
Changed in version 1.5: Added request_handler keyword parameter.
Any attempts to connect will be assumed to be from association requestors.
The server should be started with
serve_forever(poll_interval)
, where poll_interval is the timeout (in seconds) that theselect.select()
call will block for (default0.5
). A value of0
specifies a poll and never blocks. A value ofNone
blocks until a connection is ready.- ae¶
The parent AE that is running the server.
- Type:
- __init__(ae: ApplicationEntity, address: tuple[str, int], ae_title: str, contexts: list[PresentationContext], ssl_context: ssl.SSLContext | None = None, evt_handlers: list[tuple[NotificationEvent | InterventionEvent, Callable] | tuple[NotificationEvent | InterventionEvent, Callable, list[Any]]] | None = None, request_handler: Callable[[...], BaseRequestHandler] | None = None) None [source]¶
Create a new
AssociationServer
, bind a socket and start listening.- Parameters:
ae (ae.ApplicationEntity) – The parent AE that’s running the server.
address (tuple[str, int]) – The
(host: str, port: int)
that the server should run on.ae_title (str) – The AE title of the SCP.
contexts (list of presentation.PresentationContext) – The SCPs supported presentation contexts.
ssl_context (ssl.SSLContext, optional) – If TLS is to be used then this should be the
ssl.SSLContext
used to wrap the client sockets, otherwise ifNone
then no TLS will be used (default).evt_handlers (list of 2- or 3-tuple, optional) – A list of
(event, callable)
or(event, callable, args)
, the callable function to run when event occurs and the optional extra args to pass to the callable.request_handler (type) – The request handler class; an instance of this class is created for each request. Should be a subclass of
BaseRequestHandler
.
Methods
__init__
(ae, address, ae_title, contexts[, ...])Create a new
AssociationServer
, bind a socket and start listening.bind
(event, handler[, args])Bind a callable handler to an event.
close_request
(request)Called to clean up an individual request.
fileno
()Return socket file number.
finish_request
(request, client_address)Finish one request by instantiating RequestHandlerClass.
Return a list of currently bound events.
get_handlers
(event)Return handlers bound to a specific event.
Handle a connection request.
handle_error
(request, client_address)Handle an error gracefully.
handle_request
()Handle one request, possibly blocking.
handle_timeout
()Called if no new request arrives within self.timeout.
process_request
(request, client_address)Process a connection request
serve_forever
([poll_interval])Handle one request at a time until shutdown.
server_activate
()Called by constructor to activate the server.
Bind the socket and set the socket options.
Close the server.
Called by the serve_forever() loop
shutdown
()Completely shutdown the server and close it's socket.
shutdown_request
(request)Called to shutdown and close an individual request.
unbind
(event, handler)Unbind a callable handler from an event.
verify_request
(request, client_address)Verify the request.
Attributes
Return the server's running
Association
acceptor instancesaddress_family
allow_reuse_address
socket_type
Return the
ssl.SSLContext
(if available).timeout
- property active_associations: list[Association]¶
Return the server’s running
Association
acceptor instances
- bind(event: NotificationEvent | InterventionEvent, handler: Callable, args: list[Any] | None = None) None [source]¶
Bind a callable handler to an event.
Added in version 1.3.
Changed in version 1.5: Added args keyword parameter.
- Parameters:
event (namedtuple) – The event to bind the function to.
handler (callable) – The function that will be called if the event occurs.
args (list, optional) – Optional extra arguments to be passed to the handler (default: no extra arguments passed to the handler).
- get_events() list[NotificationEvent | InterventionEvent] [source]¶
Return a list of currently bound events.
Added in version 1.3.
- get_handlers(event: NotificationEvent | InterventionEvent) list[tuple[Callable, list[Any] | None]] | tuple[Callable, list[Any] | None] [source]¶
Return handlers bound to a specific event.
Added in version 1.3.
Changed in version 1.5: Returns a 2-tuple of (callable, args) or list of 2-tuple.
- Parameters:
event (namedtuple) – The event bound to the handlers.
- Returns:
If the event is a notification event then returns a list of 2-tuples containing the callable functions bound to event and the arguments passed to the callable as
(callable, args)
. If the event is an intervention event then returns either a 2-tuple of (callable, args) if a handler is bound to the event or(None, None)
if no handler has been bound.- Return type:
2-tuple of (callable, args), list of 2-tuple
- get_request() tuple[socket, tuple[str, int]] [source]¶
Handle a connection request.
If
ssl_context
is set then the client socket will be wrapped usingSSLContext.wrap_socket()
.- Returns:
client_socket (socket.socket) – The connection request.
address (2-tuple) – The client’s address as
(host, port)
.
- process_request(request: socket | tuple[bytes, socket], client_address: tuple[str, int] | str) None [source]¶
Process a connection request
- server_bind() None [source]¶
Bind the socket and set the socket options.
socket.SO_REUSEADDR
is set to1
socket.settimeout is used to set to
AE.network_timeout
unless the value isNone
in which case it will be left unset.
- property ssl_context: SSLContext | None¶
Return the
ssl.SSLContext
(if available).- Parameters:
context (ssl.SSLContext or None) – If TLS is to be used then this should be the
ssl.SSLContext
used to wrap the client sockets, otherwise ifNone
then no TLS will be used (default).
- unbind(event: NotificationEvent | InterventionEvent, handler: Callable) None [source]¶
Unbind a callable handler from an event.
Added in version 1.3.
- Parameters:
event (3-tuple) – The event to unbind the function from.
handler (callable) – The function that will no longer be called if the event occurs.