2016-06-14 4 views
1

AWS 람다를 사용하여 cronjob 스타일의 HTTP 요청을 만들고 싶습니다. 불행히도 나는 파이썬을 모른다.간단한 HTTP 요청을하는 람다 함수

나는 그들이 내가 원한 것과 비슷한 것으로 보이는 "Canary"기능을 가지고있는 것을 발견했다. 단순히 HTTP 요청을하기 위해 어떻게 단순화합니까? PHP 파일을 트리거하면됩니다.

from __future__ import print_function 

from datetime import datetime 
from urllib2 import urlopen 

SITE = 'https://www.amazon.com/' # URL of the site to check 
EXPECTED = 'Online Shopping' # String expected to be on the page 


def validate(res): 
    '''Return False to trigger the canary 

    Currently this simply checks whether the EXPECTED string is present. 
    However, you could modify this to perform any number of arbitrary 
    checks on the contents of SITE. 
    ''' 
    return EXPECTED in res 


def lambda_handler(event, context): 
    print('Checking {} at {}...'.format(SITE, event['time'])) 
    try: 
     if not validate(urlopen(SITE).read()): 
      raise Exception('Validation failed') 
    except: 
     print('Check failed!') 
     raise 
    else: 
     print('Check passed!') 
     return event['time'] 
    finally: 
     print('Check complete at {}'.format(str(datetime.now()))) 

답변

3

이 프로그램은 "단순히 HTTP 요청을합니다."

from urllib2 import urlopen 

SITE = 'https://www.amazon.com/' # URL of the site to check 

def lambda_handler(event, context): 
    urlopen(SITE) 
+0

덕분에 도움 - 나는 그런 간단한 일을 물어 그래서 바보가 된 기분하지만 난 Python을 배운 적이하고 내가 아는 모든 언어는 목록 :)에없는 –

+0

윌 주파수 설정이 실행 람다에요? 이 기능은 어디에 설정되어 있습니까? –

+0

필자는 AWS Lambda 콘솔 또는 CLI를 사용하여 빈도를 지정하는 것으로 알고 있습니다. 함수 이름을 묻는 질문에 " .lambda_handler"를 지정하면 위의 함수가 지정한 빈도로 호출됩니다. –

3

요청 (http://docs.python-requests.org/en/master/)을 사용하면보다 쉽게 ​​응답 할 수 있습니다.

import requests 

URL = 'https://www.amazon.com/' 
def lambda_handler(event, context): 
    requests.get(URL) 
+0

'요청 '은 기본 람다 환경에서는 사용할 수 없다고 생각했습니다. 그게 뭔지 아십니까? –

+0

나는 이것을 묻는 것이 어리 석다는 것을 안다.하지만이 일을 람다에서 매 1 일마다 어떻게 실행해야합니까? 커스텀 함수를 생성 할 때 카나리아 함수가 가진 cron 옵션을 가지고있는 것처럼 보이지 않습니까? 아니면 그냥 카나리아 함수를 수정합니까? –

+0

@AmyNeville,이 자습서를 읽으십시오. http://docs.aws.amazon.com/lambda/latest/dg/with-scheduledevents-example.html 또는이 참조 : http://docs.aws.amazon.com/ lambda/latest/dg/with-scheduled-events.html –