我想使用AWS lambda来制作cronjob风格的HTTP请求。不幸的是,我不了解Python。
我发现他们有一个" 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())))
答案 0 :(得分:5)
您可以使用请求(http://docs.python-requests.org/en/master/)以更轻松的方式处理响应。
import requests
URL = 'https://www.amazon.com/'
def lambda_handler(event, context):
requests.get(URL)
答案 1 :(得分:4)
该程序将“简单地发出HTTP请求。”
from urllib2 import urlopen
SITE = 'https://www.amazon.com/' # URL of the site to check
def lambda_handler(event, context):
urlopen(SITE)