pynetdicom.transport.AssociationServer

class pynetdicom.transport.AssociationServer(ae: ApplicationEntity, address: Tuple[str, int], ae_title: str, contexts: List[pynetdicom.presentation.PresentationContext], ssl_context: Optional[ssl.SSLContext] = None, evt_handlers: List[Union[Tuple[Union[NotificationEvent, InterventionEvent], Callable], Tuple[Union[NotificationEvent, InterventionEvent], Callable, List[Any]]]] = None, request_handler: Optional[Callable[[...], socketserver.BaseRequestHandler]] = None)[source]

An Association server implementation.

New 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 the select.select() call will block for (default 0.5). A value of 0 specifies a poll and never blocks. A value of None blocks until a connection is ready.

ae

The parent AE that is running the server.

Type

ae.ApplicationEntity

request_queue_size

Default 5.

Type

int

server_address

The (host: str, port: int) that the server is running on.

Type

Tuple[str, int]

__init__(ae: ApplicationEntity, address: Tuple[str, int], ae_title: str, contexts: List[pynetdicom.presentation.PresentationContext], ssl_context: Optional[ssl.SSLContext] = None, evt_handlers: List[Union[Tuple[Union[NotificationEvent, InterventionEvent], Callable], Tuple[Union[NotificationEvent, InterventionEvent], Callable, List[Any]]]] = None, request_handler: Optional[Callable[[...], socketserver.BaseRequestHandler]] = 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 if None 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.

get_events()

Return a list of currently bound events.

get_handlers(event)

Return handlers bound to a specific event.

get_request()

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.

server_bind()

Bind the socket and set the socket options.

server_close()

Close the server.

service_actions()

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

active_associations

Return the server's running Association acceptor instances

address_family

allow_reuse_address

request_queue_size

socket_type

ssl_context

Return the ssl.SSLContext (if available).

timeout

property active_associations: List[Association]

Return the server’s running Association acceptor instances

bind(event: Union[NotificationEvent, InterventionEvent], handler: Callable, args: Optional[List[Any]] = None) None[source]

Bind a callable handler to an event.

New 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[Union[NotificationEvent, InterventionEvent]][source]

Return a list of currently bound events.

New in version 1.3.

get_handlers(event: Union[NotificationEvent, InterventionEvent]) Union[List[Tuple[Callable, Optional[List[Any]]]], Tuple[Callable, Optional[List[Any]]]][source]

Return handlers bound to a specific event.

New 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.socket, Tuple[str, int]][source]

Handle a connection request.

If ssl_context is set then the client socket will be wrapped using SSLContext.wrap_socket().

Returns

  • client_socket (socket.socket) – The connection request.

  • address (2-tuple) – The client’s address as (host, port).

process_request(request: Union[socket.socket, Tuple[bytes, socket.socket]], client_address: Union[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 to 1

  • socket.settimeout is used to set to AE.network_timeout unless the value is None in which case it will be left unset.

server_close() None[source]

Close the server.

service_actions() None[source]

Called by the serve_forever() loop

shutdown() None[source]

Completely shutdown the server and close it’s socket.

property ssl_context: Optional[ssl.SSLContext]

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 if None then no TLS will be used (default).

unbind(event: Union[NotificationEvent, InterventionEvent], handler: Callable) None[source]

Unbind a callable handler from an event.

New 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.