Mercurial > hg > orthanc-tests
annotate Plugins/DicomWeb/DicomWeb.py @ 274:6b522ef4c3ec
cont
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 03 Mar 2020 17:20:13 +0100 |
parents | 943166deebcb |
children | f95cd3af1c7a |
rev | line source |
---|---|
33 | 1 #!/usr/bin/python |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
73 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
33 | 5 # Department, University Hospital of Liege, Belgium |
260
943166deebcb
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium |
33 | 7 # |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 | |
22 import os | |
23 import sys | |
24 import email | |
83 | 25 import uuid |
33 | 26 |
27 from email.mime.multipart import MIMEMultipart | |
28 from email.mime.application import MIMEApplication | |
29 | |
30 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests')) | |
31 from Toolbox import * | |
32 | |
33 | |
83 | 34 def _AttachPart(body, path, contentType, boundary): |
33 | 35 with open(path, 'rb') as f: |
83 | 36 body += bytearray('--%s\r\n' % boundary, 'ascii') |
37 body += bytearray('Content-Type: %s\r\n\r\n' % contentType, 'ascii') | |
38 body += f.read() | |
39 body += bytearray('\r\n', 'ascii') | |
33 | 40 |
41 | |
42 def SendStow(orthanc, uri, dicom): | |
83 | 43 # We do not use Python's "email" package, as it uses LF (\n) for line |
44 # endings instead of CRLF (\r\n) for binary messages, as required by | |
45 # RFC 1341 | |
46 # http://stackoverflow.com/questions/3086860/how-do-i-generate-a-multipart-mime-message-with-correct-crlf-in-python | |
47 # https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html | |
48 | |
49 # Create a multipart message whose body contains all the input DICOM files | |
50 boundary = str(uuid.uuid4()) # The boundary is a random UUID | |
51 body = bytearray() | |
33 | 52 |
53 if isinstance(dicom, list): | |
54 for i in range(dicom): | |
83 | 55 _AttachPart(body, dicom[i], 'application/dicom', boundary) |
33 | 56 else: |
83 | 57 _AttachPart(body, dicom, 'application/dicom', boundary) |
33 | 58 |
83 | 59 # Closing boundary |
60 body += bytearray('--%s--' % boundary, 'ascii') | |
33 | 61 |
83 | 62 # Do the HTTP POST request to the STOW-RS server |
63 headers = { | |
64 'Content-Type' : 'multipart/related; type=application/dicom; boundary=%s' % boundary, | |
65 'Accept' : 'application/json', | |
66 } | |
33 | 67 |
68 return DoPost(orthanc, uri, body, headers = headers) | |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
69 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
70 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
71 def DoGetMultipart(orthanc, uri, headers = {}): |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
72 answer = DoGetRaw(orthanc, uri, headers = headers) |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
73 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
74 header = '' |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
75 for i in answer[0]: |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
76 header += '%s: %s\r\n' % (i, answer[0][i]) |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
77 |
226
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
78 b = bytearray() |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
79 b.extend(header.encode('ascii')) |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
80 b.extend(b'\r\n') |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
81 b.extend(answer[1]) |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
82 |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
83 if (sys.version_info >= (3, 0)): |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
84 msg = email.message_from_bytes(b) |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
85 else: |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
86 msg = email.message_from_string(b) |
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
87 |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
88 if not msg.is_multipart(): |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
89 raise Exception('Not a multipart message') |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
90 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
91 result = [] |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
92 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
93 for part in msg.walk(): |
226
230aede7f8d5
test_bitbucket_issue_96
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
222
diff
changeset
|
94 payload = part.get_payload(decode = True) |
222
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
95 if payload != None: |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
96 result.append(payload) |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
97 |
0f03ee6ffa80
DICOMweb: test_wado_hierarchy, test_wado_bulk, test_bitbucket_issue_112
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
98 return result |