lambda_api_性能测试.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import json
  2. import boto3
  3. import time
  4. def lambda_handler(event, context):
  5. print(event)
  6. ip = f"IP {event['headers']['X-Forwarded-For']}"
  7. print(ip)
  8. mode = None
  9. array = None
  10. array_sum = None
  11. if event['httpMethod'] == 'GET':
  12. mode = 'GET'
  13. if event['multiValueQueryStringParameters'] and event['multiValueQueryStringParameters']['args']:
  14. print(event['multiValueQueryStringParameters']['args'])
  15. print(type(event['multiValueQueryStringParameters']['args']))
  16. if '[' not in event['multiValueQueryStringParameters']['args'][0]:
  17. print('list')
  18. array = [int(i) for i in event['multiValueQueryStringParameters']['args']]
  19. array_sum = sum(array)
  20. else:
  21. data = event['multiValueQueryStringParameters']['args'][0].replace('[', '').replace(']', '').replace(',', '')
  22. print(data)
  23. array = [int(i) for i in data.split()]
  24. array_sum = sum(array)
  25. elif event['httpMethod'] == 'POST':
  26. mode = 'POST'
  27. data = event['body'].replace('args=', '').split('&')
  28. array = [int(i) for i in data]
  29. array_sum = sum(array)
  30. if array_sum:
  31. result = {
  32. 'IP': ip,
  33. 'Method': mode,
  34. 'array': array,
  35. 'array_sum': array_sum,
  36. }
  37. client = boto3.client('dynamodb')
  38. try:
  39. response = client.put_item(
  40. Item={
  41. 'array': {
  42. 'S': str(array),
  43. },
  44. 'result': {
  45. 'S': str(array_sum),
  46. },
  47. 'datetime': {
  48. 'S': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
  49. }
  50. },
  51. TableName='add',
  52. )
  53. except:
  54. print('not successful')
  55. else:
  56. print(response)
  57. else:
  58. result= {
  59. "IP": ip,
  60. "Method": mode,
  61. "message": "Hello, Box"
  62. }
  63. print(result)
  64. # TODO implement
  65. return {
  66. 'statusCode': 200,
  67. 'body': json.dumps(result)
  68. }