-
Notifications
You must be signed in to change notification settings - Fork 171
/
movescp.py
executable file
·240 lines (197 loc) · 8.33 KB
/
movescp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
"""
A movescp application.
"""
import argparse
import logging
from logging.config import fileConfig
import os
import socket
import sys
import time
from pydicom import dcmread
from pydicom.dataset import Dataset
from pydicom.uid import (
ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian
)
from pynetdicom import (
AE, evt,
QueryRetrievePresentationContexts,
StoragePresentationContexts
)
VERSION = '0.4.1'
def _setup_argparser():
"""Setup the command line arguments"""
# Description
parser = argparse.ArgumentParser(
description="The movescp application implements a Service Class "
"Provider (SCP) for the Query/Retrieve (QR) Service Class "
"and the Basic Worklist Management (BWM) Service Class. "
"movescp only supports query functionality using the C-MOVE "
"message. It receives query keys from an SCU and sends a "
"response. The application can be used to test SCUs of the "
"QR and BWM Service Classes.",
usage="movescp [options] port")
# Parameters
req_opts = parser.add_argument_group('Parameters')
req_opts.add_argument("port",
help="TCP/IP port number to listen on",
type=int)
# General Options
gen_opts = parser.add_argument_group('General Options')
gen_opts.add_argument("--version",
help="print version information and exit",
action="store_true")
gen_opts.add_argument("--arguments",
help="print expanded command line arguments",
action="store_true")
gen_opts.add_argument("-q", "--quiet",
help="quiet mode, print no warnings and errors",
action="store_true")
gen_opts.add_argument("-v", "--verbose",
help="verbose mode, print processing details",
action="store_true")
gen_opts.add_argument("-d", "--debug",
help="debug mode, print debug information",
action="store_true")
gen_opts.add_argument("-ll", "--log-level", metavar='[l]',
help="use level l for the logger (fatal, error, warn, "
"info, debug, trace)",
type=str,
choices=['fatal', 'error', 'warn',
'info', 'debug', 'trace'])
gen_opts.add_argument("-lc", "--log-config", metavar='[f]',
help="use config file f for the logger",
type=str)
# Network Options
net_opts = parser.add_argument_group('Network Options')
net_opts.add_argument("-aet", "--aetitle", metavar='[a]etitle',
help="set my AE title (default: MOVESCP)",
type=str,
default='MOVESCP')
net_opts.add_argument("-to", "--timeout", metavar='[s]econds',
help="timeout for connection requests",
type=int,
default=None)
net_opts.add_argument("-ta", "--acse-timeout", metavar='[s]econds',
help="timeout for ACSE messages",
type=int,
default=60)
net_opts.add_argument("-td", "--dimse-timeout", metavar='[s]econds',
help="timeout for DIMSE messages",
type=int,
default=None)
net_opts.add_argument("-pdu", "--max-pdu", metavar='[n]umber of bytes',
help="set max receive pdu to n bytes",
type=int,
default=16384)
# Transfer Syntaxes
ts_opts = parser.add_argument_group('Preferred Transfer Syntaxes')
ts_opts.add_argument("-x=", "--prefer-uncompr",
help="prefer explicit VR local byte order (default)",
action="store_true")
ts_opts.add_argument("-xe", "--prefer-little",
help="prefer explicit VR little endian TS",
action="store_true")
ts_opts.add_argument("-xb", "--prefer-big",
help="prefer explicit VR big endian TS",
action="store_true")
ts_opts.add_argument("-xi", "--implicit",
help="accept implicit VR little endian TS only",
action="store_true")
return parser.parse_args()
args = _setup_argparser()
# Logging/Output
def setup_logger():
"""Setup the echoscu logging"""
logger = logging.Logger('movescp')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(levelname).1s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.ERROR)
return logger
APP_LOGGER = setup_logger()
def _setup_logging(level):
APP_LOGGER.setLevel(level)
pynetdicom_logger = logging.getLogger('pynetdicom')
handler = logging.StreamHandler()
pynetdicom_logger.setLevel(level)
formatter = logging.Formatter('%(levelname).1s: %(message)s')
handler.setFormatter(formatter)
pynetdicom_logger.addHandler(handler)
if args.quiet:
for hh in APP_LOGGER.handlers:
APP_LOGGER.removeHandler(hh)
APP_LOGGER.addHandler(logging.NullHandler())
if args.verbose:
_setup_logging(logging.INFO)
if args.debug:
_setup_logging(logging.DEBUG)
if args.log_level:
levels = {'critical' : logging.CRITICAL,
'error' : logging.ERROR,
'warn' : logging.WARNING,
'info' : logging.INFO,
'debug' : logging.DEBUG}
_setup_logging(levels[args.log_level])
if args.log_config:
fileConfig(args.log_config)
APP_LOGGER.debug('$movescp.py v{0!s}'.format(VERSION))
APP_LOGGER.debug('')
# Validate port
if isinstance(args.port, int):
test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
test_socket.bind((os.popen('hostname').read()[:-1], args.port))
test_socket.close()
except socket.error:
APP_LOGGER.error("Cannot listen on port {0:d}, insufficient priveleges".format(args.port))
sys.exit()
# Set Transfer Syntax options
transfer_syntax = [ImplicitVRLittleEndian,
ExplicitVRLittleEndian,
ExplicitVRBigEndian]
if args.implicit:
transfer_syntax = [ImplicitVRLittleEndian]
if args.prefer_little and ExplicitVRLittleEndian in transfer_syntax:
transfer_syntax.remove(ExplicitVRLittleEndian)
transfer_syntax.insert(0, ExplicitVRLittleEndian)
if args.prefer_big and ExplicitVRBigEndian in transfer_syntax:
transfer_syntax.remove(ExplicitVRBigEndian)
transfer_syntax.insert(0, ExplicitVRBigEndian)
def handle_move(event):
"""Implement the evt.EVT_C_MOVE handler"""
APP_DIR = os.path.join(os.path.dirname(__file__))
DATA_DIR = os.path.join(APP_DIR, '../', '../', 'tests', 'dicom_files')
dcm_files = ['RTImageStorage.dcm']
dcm_files = [os.path.join(DATA_DIR, x) for x in dcm_files]
# Address and port to send to
move_destination = event.request.MoveDestination
if move_destination == b'ANY-SCP ':
yield '127.0.0.1', 11113
else:
yield None, None
# Number of matches
yield len(dcm_files)
# Matching datasets to send
for dcm in dcm_files:
ds = dcmread(dcm, force=True)
yield 0xff00, ds
handlers = [(evt.EVT_C_MOVE, handle_move)]
# Create application entity
ae = AE(ae_title=args.aetitle)
# Add the requested Storage Service presentation contexts
# Used when sending the dataset to the move destination Storage SCP
for context in StoragePresentationContexts:
ae.add_requested_context(context.abstract_syntax, transfer_syntax)
# Add the supported QR Service presentation contexts
for context in QueryRetrievePresentationContexts:
ae.add_supported_context(context.abstract_syntax, transfer_syntax)
ae.maximum_pdu_size = args.max_pdu
# Set timeouts
ae.network_timeout = args.timeout
ae.acse_timeout = args.acse_timeout
ae.dimse_timeout = args.dimse_timeout
ae.start_server(('', args.port), evt_handlers=handlers)