pydicom / pynetdicom

A Python implementation of the DICOM networking protocol
https://pydicom.github.io/pynetdicom
MIT License
500 stars 176 forks source link

Capture the reason why failed to establish association #931

Closed evergreen-shankar closed 4 months ago

evergreen-shankar commented 4 months ago

Hi, I have an application which performs C-ECHO, C-FIND, C-STORE and C-MOVE. I would like to capture the reason when the association fails while performing these tasks. I have tried with following codebase.

from pynetdicom.sop_class import VerificationSOPClass
from pynetdicom import AE

# Initialise the Application Entity
ae = AE()

# Add a requested presentation context
ae.add_requested_context(VerificationSOPClass)

# Associate with peer AE at IP 127.0.0.1 and port 11112
try:
    assoc = ae.associate('127.0.0.1', 11112)
    if assoc.is_established:
        print("Association established successfully!")
    elif assoc.is_rejected:
        msg = ('{0}: {1}'.format(
            assoc.acceptor.primitive.result_str,
            assoc.acceptor.primitive.reason_str
        ))
        print(msg)
    elif assoc.is_aborted:
        # Capture the aborted reason
        pass
    else:
        # Capture the any other reason why failed to establish association
        print("Failed to establish association.")
except Exception as e:
    print("Failed to establish association. Reason:", e)