Forum Discussion

William_Hogan_8's avatar
William_Hogan_8
Icon for Nimbostratus rankNimbostratus
Jun 25, 2010

pycontrol and download_configuration example

After a few days of banging my head trying to get download_configuration to work within pycontrol, I finally found a solution and though I would share so maybe it will help someone else. At first I was just attempting to write the file_data straight to a file that I opened in binary mode but this always returned ASCII text file instead of the gzip compressed data that I expected. After trying a few things I found that using binascii.a2b_base64 on the file_data and then writing that output worked and the final result was a gzip compressed archive. The code below is a rough working sample without any error handling or checking.
!/usr/bin/env python

if __name__ == "__main__":
    import pycontrol.pycontrol as pc
    import binascii

    b = pc.BIGIP(hostname='xxxxxx', username='xxxxx', password='xxxx', fromurl=True, wsdls=['System.ConfigSync'])
    
    ''' Set default values '''
    defaultchunk = (64*1024)
    offset = 0
    at_eof = 0
    
    ''' Open file for writing in binary mode '''
    f = open('/tmp/config_download.ucs', 'wb')
    
    while at_eof == 0:
        ''' Grab the configuration starting at offset '''
        ctx = b.System.ConfigSync.download_configuration(config_name='XXXXXXX', chunk_size = defaultchunk, file_offset = offset)
        ''' Convert the ASCII from SOAP into binary data '''
        output = binascii.a2b_base64(ctx[0].file_data)
        ''' Write output to our file '''
        f.write(output)
        ''' Set the offset to grab on the next go '''
        offset = ctx[1]
        ''' Check for end of the file and set eof '''
        if (ctx[0].chain_type == 'FILE_LAST') or (ctx[0].chain_type == 'FILE_FIRST_AND_LAST'):
            at_eof = 1
    
    ''' We are done with writing output '''
    f.close()
No RepliesBe the first to reply