douqiu0351 2016-06-14 18:27
浏览 183

Lambda函数可以生成简单的HTTP请求

I want to use AWS lambda to make a cronjob-style HTTP request. Unfortunately I don't know Python.

I found they had a "Canary" function which seems to be similar to what I want. How do I simplify this to simply make the HTTP request? I just need to trigger a PHP file.

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())))
  • 写回答

2条回答 默认 最新

  • douyao3895 2016-06-14 18:37
    关注

    This program will "simply make the HTTP request."

    from urllib2 import urlopen
    
    SITE = 'https://www.amazon.com/'  # URL of the site to check
    
    def lambda_handler(event, context):
        urlopen(SITE)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分