annotate Plugins/DicomWeb/DicomWeb.py @ 188:7d585263808b

new test: test_queries_hierarchy
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 08 Dec 2018 18:17:10 +0100
parents 50cd127e5330
children af8e034f4262
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
73
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 33
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
130
50cd127e5330 upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 100
diff changeset
6 # Copyright (C) 2017-2018 Osimis S.A., Belgium
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # modify it under the terms of the GNU General Public License as
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # published by the Free Software Foundation, either version 3 of the
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # License, or (at your option) any later version.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful, but
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # General Public License for more details.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 #
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # You should have received a copy of the GNU General Public License
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import os
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import sys
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import email
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
25 import uuid
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 from email.mime.multipart import MIMEMultipart
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 from email.mime.application import MIMEApplication
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 from Toolbox import *
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
34 def _AttachPart(body, path, contentType, boundary):
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 with open(path, 'rb') as f:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
36 body += bytearray('--%s\r\n' % boundary, 'ascii')
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
37 body += bytearray('Content-Type: %s\r\n\r\n' % contentType, 'ascii')
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
38 body += f.read()
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
39 body += bytearray('\r\n', 'ascii')
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 def SendStow(orthanc, uri, dicom):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
43 # We do not use Python's "email" package, as it uses LF (\n) for line
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
44 # endings instead of CRLF (\r\n) for binary messages, as required by
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
45 # RFC 1341
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
46 # http://stackoverflow.com/questions/3086860/how-do-i-generate-a-multipart-mime-message-with-correct-crlf-in-python
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
47 # https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
48
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
49 # Create a multipart message whose body contains all the input DICOM files
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
50 boundary = str(uuid.uuid4()) # The boundary is a random UUID
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
51 body = bytearray()
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 if isinstance(dicom, list):
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 for i in range(dicom):
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
55 _AttachPart(body, dicom[i], 'application/dicom', boundary)
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 else:
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
57 _AttachPart(body, dicom, 'application/dicom', boundary)
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
59 # Closing boundary
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
60 body += bytearray('--%s--' % boundary, 'ascii')
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
83
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
62 # Do the HTTP POST request to the STOW-RS server
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
63 headers = {
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
64 'Content-Type' : 'multipart/related; type=application/dicom; boundary=%s' % boundary,
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
65 'Accept' : 'application/json',
3f2170efa8d2 patches for python3
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 73
diff changeset
66 }
33
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
eb6d219af210 test stow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 return DoPost(orthanc, uri, body, headers = headers)