pynetdicom: DICOM Networking Protocol

3.0.4 · active · verified Fri Apr 17

pynetdicom is a Python implementation of the DICOM networking protocol, allowing developers to create DICOM Service Class Users (SCUs) and Service Class Providers (SCPs). It handles DICOM association negotiation, message exchange, and event management. The current version is 3.0.4, and it generally follows a release cadence tied to bug fixes and minor feature enhancements, with major versions introducing significant architectural changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple DICOM Service Class User (SCU) that establishes an association with a remote DICOM Service Class Provider (SCP) and sends a C-ECHO request (a DICOM 'ping'). It uses environment variables for configuration, making it runnable without modification.

from pynetdicom import AE, VerificationPresentationContexts
import logging
import os

# Configure logging to see association details (optional but recommended)
logging.basicConfig(level=logging.INFO)

# Initialise the Application Entity (AE) for the SCU
# AE Titles must be bytes objects (e.g., b'MY_AE_TITLE')
scu_ae_title = os.environ.get('PYNETDICOM_AE_TITLE_SCU', 'PYNETDICOM_SCU').encode('utf-8')
ae = AE(ae_title=scu_ae_title)

# Add a supported presentation context for the Verification SOP Class (C-ECHO)
# This tells the AE what services it can request/provide.
ae.add_supported_context(VerificationPresentationContexts[0])

# Define the target SCP's details (can be read from environment variables for flexibility)
target_ip = os.environ.get('PYNETDICOM_TARGET_IP', '127.0.0.1')
target_port = int(os.environ.get('PYNETDICOM_TARGET_PORT', '11112'))
target_ae_title = os.environ.get('PYNETDICOM_TARGET_AE_TITLE', 'ANY_SCP_AE').encode('utf-8')

print(f"\nAttempting to associate {scu_ae_title.decode()} with {target_ae_title.decode()} at {target_ip}:{target_port}...")
print("NOTE: Ensure a DICOM SCP is running at this address and listening on the specified port.")

# Attempt to establish an association with the remote AE (SCP)
assoc = ae.associate(target_ip, target_port, ae_title=target_ae_title)

if assoc.is_established:
    print('\nAssociation established with peer.')
    # Send a C-ECHO request to verify connection
    status = assoc.send_c_echo()
    print(f'C-ECHO response status: {status}')
    # Release the association gracefully
    assoc.release()
    print('Association released.')
else:
    print('\nAssociation rejected, aborted or never connected.')

# Clean up any resources (e.g., server threads) associated with the AE
ae.shutdown()

view raw JSON →