【问题标题】:Lambda function to make simple HTTP request用于发出简单 HTTP 请求的 Lambda 函数
【发布时间】: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


    【解决方案1】:

    这个程序将“简单地发出 HTTP 请求”。

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

    【讨论】:

    • 感谢您的帮助 - 问这么简单的问题我觉得很愚蠢,但我从来没有学过 python,而且我知道的所有语言都不在他们的名单上 :)
    • 这会以 Lambda 中设置的频率运行吗?这个函数在哪里设置的?
    • 我的理解是你使用AWS Lambda控制台或者CLI来指定频率。当询问函数名时,指定“.lambda_handler”,上面的函数将按照你指定的频率被调用。
    • ""errorMessage": "无法导入模块 'lambda_function': 没有名为 'urllib2' 的模块","
    • @Jonny - 这个答案是特定于现在已经过时的 Python 2。您可能需要咨询 docs.python.org/3/library/urllib.request.html
    【解决方案2】:

    您可以使用请求 (http://docs.python-requests.org/en/master/) 以更简单的方式处理响应。

    import requests
    
    URL = 'https://www.amazon.com/'
    def lambda_handler(event, context):
        requests.get(URL)
    

    【讨论】:

    • 在我看来 requests 在默认的 lambda 环境中不可用。你知道是不是吗?
    • 我知道我问这个很愚蠢,但是我如何在 lambda 中每 1 天运行一次?创建自定义函数时,它似乎没有 canary 函数所具有的 cron 选项 - 还是我只是修改 canary 函数?
    • @Robᵩ 我觉得不行,必须安装库。
    • 请求模块必须像这样导入:from botocore.vendored import requests
    • requests 将从 botocore.vendored 中删除,如此处所述:aws.amazon.com/blogs/developer/… 虽然在此页面中提到了 10 月 19 日,但它仍然适用于我,但日志消息状态为 3/31/ 2020 年作为最终日期:此依赖项已从 Botocore 中删除,并将在 2020/03/31 之后从 Lambda 中删除
    【解决方案3】:

    我发现了一种在 AWS Lambda 中执行请求的方法。

    感谢https://stackoverflow.com/a/58994120/129202 - 这个答案还有其他几个解决方案,值得一试。下面的一个对我有用。

    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()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 2023-01-14
      • 2020-04-02
      • 2021-09-16
      • 2011-12-19
      • 2020-02-15
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多