Need python script to get message from SQS

I would like to have a python script (using boto module) which would keep listening from a given SQS (Simple Queue Service) from AWS (Amazon Web Service) and just print to standard output when any new message arrive and after printing it will delete that message from the SQS.

I am following below documentation but appreciate if I can get a working example. Thanks!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Check the github repository with the sample:

I think this does exactly what you need.
beer9Author Commented:
Appreciate if you can share some more specific code :-)
Walter RitzelSenior Software EngineerCommented:
More specific than the one I have shared? I don't think so... It is very specific, complete, and does exactly what you need...
But if you want some pointers on the code, I can help you out:
look at the code on, located at the pdf folder. There is a function to enqueue:
def queue_json_message(doc, doc_key):
    key_name =, "message-%s.json" % str(uuid4()))
    key = doc_key.bucket.new_key(key_name)
    message_data = simplejson.dumps({'bucket':, 'key':, 'uuid': doc.uuid})
    msg_body = {'bucket':, 'key':}
    queue = boto.connect_sqs(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET).create_queue(REQUEST_QUEUE)
    msg = queue.new_message(body=simplejson.dumps(msg_body))

Open in new window

and a function to read from the queue:
def _dequeue_json_message(self):
        sqs = boto.connect_sqs(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET)
        queue = sqs.create_queue(RESPONSE_QUEUE)
        msg =
        if msg is not None:
            data = simplejson.loads(msg.get_body())
            bucket = data.get('bucket', None)
            key = data.get("key", None)
            if bucket is not None and key is not None:
                return data

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.