| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import json
- import boto3
- import time
- def lambda_handler(event, context):
- print(event)
- ip = f"IP {event['headers']['X-Forwarded-For']}"
- print(ip)
- mode = None
- array = None
- array_sum = None
- if event['httpMethod'] == 'GET':
- mode = 'GET'
- if event['multiValueQueryStringParameters'] and event['multiValueQueryStringParameters']['args']:
- print(event['multiValueQueryStringParameters']['args'])
- print(type(event['multiValueQueryStringParameters']['args']))
- if '[' not in event['multiValueQueryStringParameters']['args'][0]:
- print('list')
- array = [int(i) for i in event['multiValueQueryStringParameters']['args']]
- array_sum = sum(array)
- else:
- data = event['multiValueQueryStringParameters']['args'][0].replace('[', '').replace(']', '').replace(',', '')
- print(data)
- array = [int(i) for i in data.split()]
- array_sum = sum(array)
- elif event['httpMethod'] == 'POST':
- mode = 'POST'
- data = event['body'].replace('args=', '').split('&')
- array = [int(i) for i in data]
- array_sum = sum(array)
- if array_sum:
- result = {
- 'IP': ip,
- 'Method': mode,
- 'array': array,
- 'array_sum': array_sum,
- }
- client = boto3.client('dynamodb')
- try:
- response = client.put_item(
- Item={
- 'array': {
- 'S': str(array),
- },
- 'result': {
- 'S': str(array_sum),
- },
- 'datetime': {
- 'S': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
- }
- },
- TableName='add',
- )
- except:
- print('not successful')
- else:
- print(response)
- else:
- result= {
- "IP": ip,
- "Method": mode,
- "message": "Hello, Box"
- }
- print(result)
- # TODO implement
- return {
- 'statusCode': 200,
- 'body': json.dumps(result)
- }
|