comparison Resources/Samples/ImportDicomFiles/ImportDicomFiles.py @ 370:22bb88181e06

credentials
author jodogne
date Tue, 12 Mar 2013 11:14:45 +0100
parents 77e526e6fdf8
children 7000726bade7
comparison
equal deleted inserted replaced
369:4632a044746e 370:22bb88181e06
3 import os 3 import os
4 import sys 4 import sys
5 import os.path 5 import os.path
6 import httplib2 6 import httplib2
7 7
8 if len(sys.argv) != 4: 8 if len(sys.argv) != 4 and len(sys.argv) != 6:
9 print(""" 9 print("""
10 Sample script to recursively import in Orthanc all the DICOM files 10 Sample script to recursively import in Orthanc all the DICOM files
11 that are stored in some path. Please make sure that Orthanc is running 11 that are stored in some path. Please make sure that Orthanc is running
12 before starting this script. The files are uploaded through the REST 12 before starting this script. The files are uploaded through the REST
13 API. 13 API.
14 14
15 Usage: %s [hostname] [HTTP port] [path] 15 Usage: %s [hostname] [HTTP port] [path]
16 Usage: %s [hostname] [HTTP port] [path] [username] [password]
16 For instance: %s localhost 8042 . 17 For instance: %s localhost 8042 .
17 """ % (sys.argv[0], sys.argv[0])) 18 """ % (sys.argv[0], sys.argv[0], sys.argv[0]))
18 exit(-1) 19 exit(-1)
19 20
20 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2])) 21 URL = 'http://%s:%d/instances' % (sys.argv[1], int(sys.argv[2]))
21 22
22 success = 0 23 success = 0
24 25
25 # This function will upload a single file to Orthanc through the REST API 26 # This function will upload a single file to Orthanc through the REST API
26 def UploadFile(path): 27 def UploadFile(path):
27 global success 28 global success
28 29
29 f = open(path, "r") 30 f = open(path, "rb")
30 content = f.read() 31 content = f.read()
31 f.close() 32 f.close()
32 33
33 try: 34 try:
34 sys.stdout.write("Importing %s" % path) 35 sys.stdout.write("Importing %s" % path)
35 36
36 h = httplib2.Http() 37 h = httplib2.Http()
38
39 if len(sys.argv) == 6:
40 h.add_credentials(sys.argv[4], sys.argv[5])
41
37 resp, content = h.request(URL, 'POST', 42 resp, content = h.request(URL, 'POST',
38 body = content, 43 body = content,
39 headers = { 'content-type' : 'application/dicom' }) 44 headers = { 'content-type' : 'application/dicom' })
40 45
41 if resp.status == 200: 46 if resp.status == 200:
43 success += 1 48 success += 1
44 else: 49 else:
45 sys.stdout.write(" => failure (is it a DICOM file?)\n") 50 sys.stdout.write(" => failure (is it a DICOM file?)\n")
46 51
47 except: 52 except:
48 sys.stdout.write(" => unable to connect\n") 53 sys.stdout.write(" => unable to connect (is there a password?)\n")
49 54
50 55
51 if os.path.isfile(sys.argv[3]): 56 if os.path.isfile(sys.argv[3]):
52 # Upload a single file 57 # Upload a single file
53 UploadFile(sys.argv[3]) 58 UploadFile(sys.argv[3])