Лямбда-функция для создания простого HTTP-запроса

Я хочу использовать AWS lambda для выполнения HTTP-запроса в стиле cronjob. К сожалению, я не знаю 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 pagedef 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 resdef 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

Решение

Вы можете использовать Запросы (http://docs.python-requests.org/en/master/) работать с ответом проще.

import requests

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

Другие решения

Эта программа «просто сделает запрос HTTP».

from urllib2 import urlopen

SITE = 'https://www.amazon.com/'  # URL of the site to check

def lambda_handler(event, context):
urlopen(SITE)
4

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector