【发布时间】:2022-02-15 21:37:21
【问题描述】:
我想使用 AWS lambda 发出一个 cronjob 样式的 HTTP 请求。可惜我不懂 Python。
我发现他们有一个“金丝雀”功能,这似乎与我想要的相似。我如何简化这个以简单地发出 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())))
【问题讨论】:
标签: php python cron aws-lambda