2016-06-14 14 views
1

AWSラムダを使用してcronjobスタイルのHTTPリクエストを作成したいと考えています。残念ながら私はPythonを知らない。単純なHTTPリクエストを行うLambda関数

私は彼らが私が望むものに似ているような "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ラムダコンソールまたは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日ごとにこれを実行するにはどうすればいいですか?カスタム関数を作成するとき、canary関数が持っていたcronオプションを持っていないようです - あるいは単にcanary関数を変更しますか? –

+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 –

関連する問題