Skip to content

Instantly share code, notes, and snippets.

@rviki84
Created January 28, 2021 00:34
Show Gist options
  • Select an option

  • Save rviki84/b1f85cc29a2785d17e0a2dfe10381c71 to your computer and use it in GitHub Desktop.

Select an option

Save rviki84/b1f85cc29a2785d17e0a2dfe10381c71 to your computer and use it in GitHub Desktop.
XNAT file upload in python
#
# Basic XNAT file upload with standard python
#
import httplib, urlparse, mimetypes, io, base64, urllib, os, sys
def createResource(host, selector, auth):
"""
Create an XNAT resource (directory)
Return the server's response page.
"""
body = ''
h = httplib.HTTP(host)
h.putrequest('PUT', selector + '?inbody=true')
h.putheader('Authorization', 'Basic ' + base64.b64encode( auth))
h.putheader('content-type', 'application/octet-stream')
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()
def uploadFile(host, selector, auth, body):
"""
Upload a file as specified by the selector
Body is the file contents
Return the server's response page.
"""
h = httplib.HTTP(host)
h.putrequest('PUT', selector + '?inbody=true')
h.putheader('Authorization', 'Basic ' + base64.b64encode( auth))
h.putheader('content-type', 'application/octet-stream')
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()
##############################################
## XNAT Server Configuration Parameters ##
xnatHost = 'project-xnat.institute.org:8080' # replace it with actual host
xnatRoot = '/xnat' # replace it with actual root directory
xnatUser = 'username' # !replace it with actual username
xnatPw = 'password' # !replace it with actual password
xnatAuth = xnatUser + ':' + xnatPw
## File Upload Parameters ##
xnatProject = 'test'
xnatSubject = 'SUB001'
xnatRemoteFile = 'remFile.csv'
localFilename = 'locFile.csv'
########### upload code #############
xnatPath = '/data/archive/projects/' + xnatProject \
+ '/subjects/' + xnatSubject \
+ '/resources/SIM'
xnatSelector = xnatRoot + xnatPath
if 0:
deleteResource( xnatHost, xnatSelector, xnatAuth)
print 'attempting resource deletion'
print '\t%s'% xnatPath
exit(0)
# check the resource existence by listing it
# if fail, create resource
try:
# get SIM url
resUrl = 'http://%s@%s%s' \
% (xnatAuth, xnatHost, xnatSelector)
print 'checking resource %s' % xnatSelector
resObj = urllib.urlopen( resUrl)
resObj.close()
if( resObj.getcode() == 404):
print 'attempting resource creation'
print '\t%s'% xnatPath
createResource( xnatHost, xnatSelector, xnatAuth)
except:
(exc_type, exc_value, exc_traceback) = sys.exc_info()
print "Problems checking/creating resource"
print exc_value
# upload file
xnatFileSelector = xnatSelector + '/files/' + xnatRemoteFile
print 'attempting file upload'
print '\t%s' % xnatFileSelector
with open( localFilename, 'rb') as inFile:
uploadFile( xnatHost, xnatFileSelector, xnatAuth, inFile.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment