Mercurial > hg > orthanc
comparison OrthancServer/Resources/Samples/Python/Replicate.py @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Resources/Samples/Python/Replicate.py@94f4a18a79cc |
children | d9473bd5ed43 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
5 # Department, University Hospital of Liege, Belgium | |
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium | |
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 base64 | |
23 import httplib2 | |
24 import json | |
25 import re | |
26 import sys | |
27 | |
28 URL_REGEX = re.compile('(http|https)://((.+?):(.+?)@|)(.*)') | |
29 | |
30 | |
31 if len(sys.argv) != 3: | |
32 print(""" | |
33 Script to copy the content of one Orthanc server to another Orthanc | |
34 server through their REST API. | |
35 | |
36 Usage: %s [SourceURI] [TargetURI] | |
37 For instance: %s http://orthanc:password@127.0.0.1:8042/ http://127.0.0.1:8043/ | |
38 """ % (sys.argv[0], sys.argv[0])) | |
39 exit(-1) | |
40 | |
41 | |
42 | |
43 def CreateHeaders(parsedUrl): | |
44 headers = { } | |
45 username = parsedUrl.group(3) | |
46 password = parsedUrl.group(4) | |
47 | |
48 if username != None and password != None: | |
49 # This is a custom reimplementation of the | |
50 # "Http.add_credentials()" method for Basic HTTP Access | |
51 # Authentication (for some weird reason, this method does not | |
52 # always work) | |
53 # http://en.wikipedia.org/wiki/Basic_access_authentication | |
54 headers['authorization'] = 'Basic ' + base64.b64encode(username + ':' + password) | |
55 | |
56 return headers | |
57 | |
58 | |
59 def GetBaseUrl(parsedUrl): | |
60 return '%s://%s' % (parsedUrl.group(1), parsedUrl.group(5)) | |
61 | |
62 | |
63 def DoGetString(url): | |
64 global URL_REGEX | |
65 parsedUrl = URL_REGEX.match(url) | |
66 headers = CreateHeaders(parsedUrl) | |
67 | |
68 h = httplib2.Http() | |
69 resp, content = h.request(GetBaseUrl(parsedUrl), 'GET', headers = headers) | |
70 | |
71 if resp.status == 200: | |
72 return content | |
73 else: | |
74 raise Exception('Unable to contact Orthanc at: ' + url) | |
75 | |
76 | |
77 def DoPostDicom(url, body): | |
78 global URL_REGEX | |
79 parsedUrl = URL_REGEX.match(url) | |
80 headers = CreateHeaders(parsedUrl) | |
81 headers['content-type'] = 'application/dicom' | |
82 | |
83 h = httplib2.Http() | |
84 resp, content = h.request(GetBaseUrl(parsedUrl), 'POST', | |
85 body = body, | |
86 headers = headers) | |
87 | |
88 if resp.status != 200: | |
89 raise Exception('Unable to contact Orthanc at: ' + url) | |
90 | |
91 | |
92 def _DecodeJson(s): | |
93 if (sys.version_info >= (3, 0)): | |
94 return json.loads(s.decode()) | |
95 else: | |
96 return json.loads(s) | |
97 | |
98 | |
99 def DoGetJson(url): | |
100 return _DecodeJson(DoGetString(url)) | |
101 | |
102 | |
103 SOURCE = sys.argv[1] | |
104 TARGET = sys.argv[2] | |
105 | |
106 for study in DoGetJson('%s/studies' % SOURCE): | |
107 print('Sending study %s...' % study) | |
108 for instance in DoGetJson('%s/studies/%s/instances' % (SOURCE, study)): | |
109 dicom = DoGetString('%s/instances/%s/file' % (SOURCE, instance['ID'])) | |
110 DoPostDicom('%s/instances' % TARGET, dicom) |