Amy*_*lle 2 php python cron aws-lambda
我想使用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())))
Run Code Online (Sandbox Code Playgroud)
该程序将“简单地发出 HTTP 请求”。
from urllib2 import urlopen
SITE = 'https://www.amazon.com/' # URL of the site to check
def lambda_handler(event, context):
urlopen(SITE)
Run Code Online (Sandbox Code Playgroud)
我找到了一种在 AWS Lambda 中执行请求的方法。
感谢/sf/answers/4129588431/ - 这个答案还有一些其他解决方案,可能值得一试。下面的一项对我有用。
import urllib.request
import json
res = urllib.request.urlopen(urllib.request.Request(
url='http://asdfast.beobit.net/api/',
headers={'Accept': 'application/json'},
method='GET'),
timeout=5)
print(res.status)
print(res.reason)
print(json.loads(res.read()))
Run Code Online (Sandbox Code Playgroud)
您可以使用请求(http://docs.python-requests.org/en/master/)以更简单的方式处理响应.
import requests
URL = 'https://www.amazon.com/'
def lambda_handler(event, context):
requests.get(URL)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6764 次 |
| 最近记录: |