comparison Plugins/DicomWeb/DicomWeb.py @ 33:eb6d219af210

test stow
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 02 Aug 2015 14:01:32 +0200
parents
children 97acfdf0dbce
comparison
equal deleted inserted replaced
32:682c4e2a1162 33:eb6d219af210
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 #
7 # This program is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 import os
22 import sys
23 import email
24 import urllib2
25
26 from email.mime.multipart import MIMEMultipart
27 from email.mime.application import MIMEApplication
28
29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
30 from Toolbox import *
31
32
33 def _AttachPart(related, path, contentType):
34 with open(path, 'rb') as f:
35 part = MIMEApplication(f.read(), contentType, email.encoders.encode_noop)
36 related.attach(part)
37
38
39 def SendStow(orthanc, uri, dicom):
40 related = MIMEMultipart('related')
41 related.set_boundary('boundary_0123456789_boundary')
42
43 if isinstance(dicom, list):
44 for i in range(dicom):
45 _AttachPart(related, dicom[i], 'dicom')
46 else:
47 _AttachPart(related, dicom, 'dicom')
48
49 headers = dict(related.items())
50 body = related.as_string()
51
52 # Discard the header
53 body = body.split('\n\n', 1)[1]
54
55 headers['Content-Type'] = 'multipart/related; type=application/dicom; boundary=%s' % related.get_boundary()
56 headers['Accept'] = 'application/json'
57
58 return DoPost(orthanc, uri, body, headers = headers)
59
60
61 def GetMultipart(uri, headers = {}):
62 tmp = urllib2.urlopen(uri)
63 info = str(tmp.info())
64 answer = tmp.read()
65
66 s = info + "\n" + answer
67
68 msg = email.message_from_string(s)
69
70 result = []
71
72 for i, part in enumerate(msg.walk(), 1):
73 payload = part.get_payload(decode = True)
74 if payload != None:
75 result.append(payload)
76
77 return result