comparison Plugins/DicomWeb/DicomWeb.py @ 83:3f2170efa8d2

patches for python3
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 23 Jun 2016 18:04:59 +0200
parents 97acfdf0dbce
children 2af6c0fb850d
comparison
equal deleted inserted replaced
82:91e2ed032f96 83:3f2170efa8d2
19 19
20 20
21 import os 21 import os
22 import sys 22 import sys
23 import email 23 import email
24 import urllib2 24 import uuid
25 25
26 from email.mime.multipart import MIMEMultipart 26 from email.mime.multipart import MIMEMultipart
27 from email.mime.application import MIMEApplication 27 from email.mime.application import MIMEApplication
28 28
29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests')) 29 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
30 from Toolbox import * 30 from Toolbox import *
31 31
32 32
33 def _AttachPart(related, path, contentType): 33 def _AttachPart(body, path, contentType, boundary):
34 with open(path, 'rb') as f: 34 with open(path, 'rb') as f:
35 part = MIMEApplication(f.read(), contentType, email.encoders.encode_noop) 35 body += bytearray('--%s\r\n' % boundary, 'ascii')
36 related.attach(part) 36 body += bytearray('Content-Type: %s\r\n\r\n' % contentType, 'ascii')
37 body += f.read()
38 body += bytearray('\r\n', 'ascii')
37 39
38 40
39 def SendStow(orthanc, uri, dicom): 41 def SendStow(orthanc, uri, dicom):
40 related = MIMEMultipart('related') 42 # We do not use Python's "email" package, as it uses LF (\n) for line
41 related.set_boundary('boundary_0123456789_boundary') 43 # endings instead of CRLF (\r\n) for binary messages, as required by
44 # RFC 1341
45 # http://stackoverflow.com/questions/3086860/how-do-i-generate-a-multipart-mime-message-with-correct-crlf-in-python
46 # https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
47
48 # Create a multipart message whose body contains all the input DICOM files
49 boundary = str(uuid.uuid4()) # The boundary is a random UUID
50 body = bytearray()
42 51
43 if isinstance(dicom, list): 52 if isinstance(dicom, list):
44 for i in range(dicom): 53 for i in range(dicom):
45 _AttachPart(related, dicom[i], 'dicom') 54 _AttachPart(body, dicom[i], 'application/dicom', boundary)
46 else: 55 else:
47 _AttachPart(related, dicom, 'dicom') 56 _AttachPart(body, dicom, 'application/dicom', boundary)
48 57
49 headers = dict(related.items()) 58 # Closing boundary
50 body = related.as_string() 59 body += bytearray('--%s--' % boundary, 'ascii')
51 60
52 # Discard the header 61 # Do the HTTP POST request to the STOW-RS server
53 body = body.split('\n\n', 1)[1] 62 headers = {
54 63 'Content-Type' : 'multipart/related; type=application/dicom; boundary=%s' % boundary,
55 headers['Content-Type'] = 'multipart/related; type=application/dicom; boundary=%s' % related.get_boundary() 64 'Accept' : 'application/json',
56 headers['Accept'] = 'application/json' 65 }
57 66
58 return DoPost(orthanc, uri, body, headers = headers) 67 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