Skip to content

Instantly share code, notes, and snippets.

@MichaelSEA
Created February 24, 2016 21:13
Show Gist options
  • Save MichaelSEA/28bc5071b8152f4262d4 to your computer and use it in GitHub Desktop.
Save MichaelSEA/28bc5071b8152f4262d4 to your computer and use it in GitHub Desktop.
handle re-auth
import sys
import tempfile
import time
import qumulo.lib.auth
import qumulo.lib.request
import qumulo.rest.fs as fs
host = 'myhost'
port = 8000
user = 'user'
passwd = 'pwd'
filepath = '/users/user/stuff.txt'
def login(host, user, passwd, port):
'''Obtain credentials from the REST server'''
connection = None
credentials = None
try:
# Create a connection to the REST server
connection = qumulo.lib.request.Connection(host, int(port))
# Provide username and password to retreive authentication tokens
# used by the credentials object
login_results, _ = qumulo.rest.auth.login(
connection, None, user, passwd)
# Create the credentials object which will be used for
# authenticating rest calls
credentials = qumulo.lib.auth.Credentials.from_login_response(login_results)
except Exception, excpt:
print "Error connecting to the REST server: %s" % excpt
print __doc__
sys.exit(1)
return connection, credentials
connection = None
credentials = None
while (True):
try:
# do some stuff, like read from the file system
# get file contents for a well-known file
file = tempfile.TemporaryFile()
fs.read_file(connection, credentials, file, path=filepath, id_=None)
print "been there, read that..."
file.close()
time.sleep(60)
except (qumulo.lib.request.RequestError, AttributeError):
# auth failures usually return a RequestError with Error 401 I think... try logging in again
connection, credentials = login(host, user, passwd, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment