Skip to content

Instantly share code, notes, and snippets.

@dasgoll
Created August 22, 2015 08:04
Show Gist options
  • Save dasgoll/25c06cea3f59d27878eb to your computer and use it in GitHub Desktop.
Save dasgoll/25c06cea3f59d27878eb to your computer and use it in GitHub Desktop.
In [1]: import boto
In [2]: sns = boto.connect_sns()
In [3]: sns
Out[3]: SNSConnection:sns.us-east-1.amazonaws.com
In [4]: topics = sns.get_all_topics()
Out[4]:
{u'ListTopicsResponse': {u'ListTopicsResult': {u'NextToken': None,
u'Topics': [{u'TopicArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms'}]},
u'ResponseMetadata': {u'RequestId': u'123123xxx-05fa-11e1-90f8-6bc47860e188'}}}
In [11]: mytopics = topics["ListTopicsResponse"]["ListTopicsResult"]["Topics"]
In [12]: mytopics
Out[12]: [{u'TopicArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms'}]
In [14]: mytopic_arn = mytopics[0]["TopicArn"]
In [15]: mytopic_arn
Out[15]: u'arn:aws:sns:us-east-1:123123xxx165:test_sms'
In [16]: subscriptions = sns.get_all_subscriptions_by_topic(mytopic_arn)
In [17]: subscriptions
Out[17]:
{u'ListSubscriptionsByTopicResponse': {u'ListSubscriptionsByTopicResult': {u'NextToken': None,
u'Subscriptions': [{u'Endpoint': u'[email protected]',
u'Owner': u'xxxxxx879165',
u'Protocol': u'email',
u'SubscriptionArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms:ac39a0a7-0350-4226-b8f1-064d55540e45',
u'TopicArn': u'arn:aws:sns:us-east-1:230477879165:test_sms'}]},
u'ResponseMetadata': {u'RequestId': u'5cae1c85-05fb-11e1-945d-17a52825d3d9'}}}
In [18]: subscriptions = sns.get_all_subscriptions_by_topic(mytopic_arn)
In [19]: subscriptions
Out[19]:
{u'ListSubscriptionsByTopicResponse': {u'ListSubscriptionsByTopicResult': {u'NextToken': None,
u'Subscriptions': [{u'Endpoint': u'[email protected]',
u'Owner': u'xxxxxx879165',
u'Protocol': u'email',
u'SubscriptionArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms:xxxxxxc3-3c89-4bac-ac9d-44ff681ffc20',
u'TopicArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms'},
{u'Endpoint': u'[email protected]',
u'Owner': u'xxxxxx879165',
u'Protocol': u'email',
u'SubscriptionArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms:xxxxxxa7-0350-4226-b8f1-064d55540e45',
u'TopicArn': u'arn:aws:sns:us-east-1:123123xxx165:test_sms'}]},
u'ResponseMetadata': {u'RequestId': u'xxxxxxe4-05fb-11e1-945d-17a52825d3d9'}}}
In [20]: msg = "Hi there\nI am sending this message over boto.\nYour booty Jan"
In [21]: subj = "SNS message over boto"
In [23]: res = sns.publish(mytopic_arn, msg, subj)
In [24]: res
Out[24]:
{u'PublishResponse': {u'PublishResult': {u'MessageId': u'xxxxxx3a-e105-455f-a279-ef43ec1bcf01'},
u'ResponseMetadata': {u'RequestId': u'xxxxxx5e-05fc-11e1-90f8-6bc47860e188'}}}
In [26]: sns.subscribe(mytopic_arn, "email", "[email protected]")
Out[26]:
{u'SubscribeResponse': {u'ResponseMetadata': {u'RequestId': u'xxxxxxfd-05fc-11e1-9605-ff1e67cc7ab7'},
u'SubscribeResult': {u'SubscriptionArn': u'pending confirmation'}}}
In [27]: res = sns.publish(mytopic_arn, msg, "Second msg over boto")
In [28]: res
Out[28]:
{u'PublishResponse': {u'PublishResult': {u'MessageId': u'xxxxxxe9-d42b-4d0e-9b9d-7e19d8c52d93'},
u'ResponseMetadata': {u'RequestId': u'xxxxxxdd-05fd-11e1-945d-17a52825d3d9'}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment