-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5.py
63 lines (53 loc) · 1.61 KB
/
ex5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import boto3
import logging
sqs = boto3.client('sqs')
def send_message_to_sqs(cat, url):
response = sqs.send_message(
QueueUrl=url,
DelaySeconds=10,
MessageAttributes={
'PetName': {
'DataType': 'String',
'StringValue': cat['cat_name']
},
'PetId': {
'DataType': 'Number',
'StringValue': str(cat['cat_id'])
},
'Status': {
'DataType': 'String',
'StringValue': cat['status']
}
},
MessageBody=('body')
)
return response['MessageId']
def read_message_from_sqs(url):
retval = None
# Read message from SQS queue.
response = sqs.receive_message(
QueueUrl=url,
AttributeNames=['SentTimestamp'],
MaxNumberOfMessages=1,
MessageAttributeNames=['All'],
VisibilityTimeout=30,
WaitTimeSeconds=0
)
message = None
try:
message = response['Messages'][0] # Only read one.
except KeyError as ke:
logging.info("SQS queue is empty.")
if message:
retval = {
"Status": message["MessageAttributes"]["Status"]["StringValue"],
"PetId": message["MessageAttributes"]["PetId"]["StringValue"],
"PetName": message["MessageAttributes"]["PetName"]["StringValue"],
}
# Delete message once we have read it from the queue.
receipt_handle = message['ReceiptHandle']
sqs.delete_message(
QueueUrl=url,
ReceiptHandle=receipt_handle
)
return retval