Connect to Elastic MQ with boto

Connect to Elastic MQ with boto

A local replacement for Amazon SQS.

I often see the method using connect_to_region of boto, If it is a local ElasticMQ, you have to connect with regioninfo.SQSRegionInfo, so make a note of it.

It seems that if ʻis_secure is set to True, it becomes https`.

Preparation

ElasticMQ is dropped and started. By default, the port is 9324.

$ java -jar ~/opt/elasticmq-server-0.8.8.jar

The one to send

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from boto.sqs import regioninfo
from boto.sqs.message import Message

AWS_ACCESS_KEY = 'TEST'   #Any EMQ is fine
AWS_SECRET_KEY = 'TEST'   #Any EMQ is fine

SQS_REGION     = "emq"         #Any EMQ is fine
SQS_ENDPOINT   = "localhost"

#For AWS
# SQS_REGION = "ap-northeast-1"
# SQS_ENDPOINT = "sqs.ap-northeast-1.amazonaws.com"

SQS_QUEUE_NAME = 'dev_messages'
PORT = 9324     #80 for AWS/Or do not specify

region = regioninfo.SQSRegionInfo(name=SQS_REGION, endpoint=SQS_ENDPOINT)
conn = region.connect(
        aws_access_key_id=AWS_ACCESS_KEY,
        aws_secret_access_key=AWS_SECRET_KEY,
        port=PORT,
        is_secure=False
    );

queue = conn.create_queue(SQS_QUEUE_NAME)
msg = Message(
    body='QUEUE TEST',
)
msg.message_attributes = {
        "name1": {
            "data_type": "String",
            "string_value": "I am a string"
        },
    }
queue.write(msg)

The one who receives

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from boto.sqs import regioninfo
from boto.sqs.message import Message

AWS_ACCESS_KEY = 'TEST'   #Any EMQ is fine
AWS_SECRET_KEY = 'TEST'   #Any EMQ is fine

SQS_REGION     = "emq"         #Any EMQ is fine
SQS_ENDPOINT   = "localhost"

#For AWS
# SQS_REGION = "ap-northeast-1"
# SQS_ENDPOINT = "sqs.ap-northeast-1.amazonaws.com"

SQS_QUEUE_NAME = 'dev_messages'
PORT = 9324     #80 for AWS/Or do not specify

region = regioninfo.SQSRegionInfo(name=SQS_REGION, endpoint=SQS_ENDPOINT)
conn = region.connect(
        aws_access_key_id=AWS_ACCESS_KEY,
        aws_secret_access_key=AWS_SECRET_KEY,
        port=PORT,
        is_secure=False
    );

queue = conn.get_queue(SQS_QUEUE_NAME)
queue.set_attribute('ReceiveMessageWaitTimeSeconds', 20)

import json

msgs = queue.get_messages(10)
for msg in msgs:
    print json.dumps(msg.__dict__, default=str)
    queue.delete_message(msg)

Recommended Posts

Connect to Elastic MQ with boto
Connect to BigQuery with Python
Connect to Wikipedia with Python
Connect to Postgresql with GO
Connect to multiple databases with SQLAlchemy
Connect to Bitcoin Testnet with Pycoin
Use boto3 to mess with S3
Connect to MySQL with Python within Docker
Connect to GNU / Linux with Remote Desktop
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
How to use SQLAlchemy / Connect with aiomysql
Connect to mysql
Easily connect Xillybus to user logic with cReComp
Connect to MySQL with Python on Raspberry Pi
Tested with boto3 + mock
Region specification with boto
S3 uploader with boto
Connect python to mysql
read the tag assigned to you on ec2 with boto3
Connect to centos6 on virtualbox with ssh connection from Mac
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
I wanted to delete multiple objects in s3 with boto3
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Try to factorial with recursion
Set connection timeout with boto3
Post to slack with Python 3
S3 operation with python boto3
Output to syslog with Loguru
Introduction to RDB with sqlalchemy Ⅰ
Easy to make with syntax
How to update with SQLAlchemy?
Connect Vagrant's MySQL with MySQL Workbench
Change retry settings with boto3
How to cast with Theano
How to Alter with SQLAlchemy?
Connect to sqlite from python
How to separate strings with','
Connect to s3 tokyo region
How to RDP with Fedora31
2 ways to deal with SessionNotCreatedException
How to Delete with SQLAlchemy?
How to connect to Cloud Firestore from Google Cloud Functions with python code