Python Fax - Retrieve Inbound Fax Image

Download the Python samples fax API zip file.
Download the Modified OSA files.

In this sample, you can learn how to retrieve an image for an inbound (received) fax with Python. This is done using the GetImageChunk method.

This Python fax snippet demonstrates how to retrieve a fax image in multiple chunks (the chunk size is configurable).

Use the GetList method to retrieve the fax message ID and file size (both are needed in order to retrieve the fax image).


"""
A simple script to fetch a chunk of a received fax image
using the InterFAX GetImageChunk SOAP API call.
"""
from interfax import client

print 'Testing GetImageChunk...'
c = client.InterFaxClient('USERNAME','PASSWORD') # Enter your Interfax username/password here

messageId = 12345678 # Enter the ID of the inbound message to be read
messageSize = 2931   # Size in bytes of the inbound message. This value
                     # is returned by method GetList, which should be run
                     # before this one
result = c.getImageChunk( messageId, True, messageSize, 0, "/tmp/%d.tif" % messageId )
print '   GetImageChunk returned with code %d' % result"""
A simple script to fetch a chunk of a received fax image
using the InterFAX GetImageChunk SOAP API call.
"""
from osa import Client
import  base64
import sys

# helper function to save file data
def save_file(file_name, file_data):
    ext = ".pdf" if   file_data.startswith(b"%PDF") else ".tif"
    print ("\tSaving " + file_name + ext)
    with open(file_name + ext,"wb") as f:
        f.write(file_data)

# Create a SoapClient with a link to the WSDL definition.
client =  Client("http://ws.interfax.net/inbound.asmx?wsdl")

print('Testing GetImageChunk...')

chunk_size = 1024 #chunk size can be increased to 100000

message_id = 282535702  # Enter the ID of the inbound message to be read is returned by method GetList, which should be run before this one

start_from = 0
image_data = bytes()

get_image_chunk_result = 0
while get_image_chunk_result == 0:
    result = client.service.GetImageChunk(Username='', Password='', MessageID= message_id, MarkAsRead= False, ChunkSize=chunk_size, From=start_from)
    get_image_chunk_result = result.GetImageChunkResult
    print('   GetImageChunk returned with code {0}'.format(get_image_chunk_result))
    if get_image_chunk_result == 0:
            image_data +=  result.Image
            start_from += chunk_size
    if get_image_chunk_result ==0:
        continue
    if get_image_chunk_result == -3005:
        print ('Complete image received.')
    else:
        print ('Error Getting Image Chunk')

if len(image_data) > 0:
    save_file('out_file',image_data)

Contact us today

Talk to a member of our team about the benefits InterFAX can bring to your organization's communications processes.

Contact us today