pyControl v2 Send a File

Problem this snippet solves:

The following code takes a connected pycontrol.pycontrol.BIGIP object and uploads a file:

Code :

def send_file(obj, local_file):
    import io, base64
    ctx = obj.System.ConfigSync.typefactory.create(
                           'System.ConfigSync.FileTransferContext')
    poll = True
    chunk_size = 64*768
    ctx.chain_type = 'FILE_FIRST'
    tsent = 0
    try:
        f = io.open(local_file, 'rb')
    except IOError, e:
        print >> sys.stderr, e
        sys.exit(1)
    while poll:
        fdata = f.read(chunk_size)
        if len(fdata) != chunk_size:
            if tsent == 0:
                ctx.chain_type = 'FILE_FIRST_AND_LAST'
            else:
                ctx.chain_type = 'FILE_LAST'
            poll = False
        ctx.file_data = base64.b64encode(fdata)
        obj.System.ConfigSync.upload_configuration(local_file, ctx)
        tsent += 1
        ctx.chain_type = 'FILE_MIDDLE'
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment