Skip to content

Instantly share code, notes, and snippets.

@iMilnb
Created August 2, 2015 20:20
Show Gist options
  • Save iMilnb/bf27da3f38272a76c801 to your computer and use it in GitHub Desktop.
Save iMilnb/bf27da3f38272a76c801 to your computer and use it in GitHub Desktop.
Basic Flask snippet to handle AWS SNS messages and subscription
from flask import Flask, request
import requests
import json
app = Flask(__name__)
def msg_process(msg, tstamp):
js = json.loads(msg)
msg = 'Region: {0} / Alarm: {1}'.format(
js['Region'], js['AlarmName']
)
# do stuff here, like calling your favorite SMS gateway API
@app.route('/', methods = ['GET', 'POST', 'PUT'])
def sns():
# AWS sends JSON with text/plain mimetype
try:
js = json.loads(request.data)
except:
pass
hdr = request.headers.get('X-Amz-Sns-Message-Type')
# subscribe to the SNS topic
if hdr == 'SubscriptionConfirmation' and 'SubscribeURL' in js:
r = requests.get(js['SubscribeURL'])
if hdr == 'Notification':
msg_process(js['Message'], js['Timestamp'])
return 'OK\n'
if __name__ == '__main__':
app.run(
host = "0.0.0.0",
port = 5000,
debug = True
)
@ankitoye
Copy link

ankitoye commented Oct 7, 2018

Thank you for sharing this code. I am getting following error while parsing a Notification, subscription is successful. Please help.

x.x.x.x - - [07/Oct/2018 08:44:44] "POST /login HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/.local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/opt/project2app.py", line 33, in sns
    msg_process(js['Subject'], js['Message'])
  File "/opt/project2app.py", line 10, in msg_process
    js = json.loads(msg)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

@cdosborn
Copy link

Thank you, works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment