用python接受urls中的params

时间:2017-05-25 02:55:20

标签: python python-requests aws-lambda mailgun

我将在AWS Lambda函数中托管以下代码。

def indoctrination(params=params):
time1 = datetime.utcnow() + timedelta(days=1)
time2 = datetime.utcnow() + timedelta(days=2)
time3 = datetime.utcnow() + timedelta(days=3)
requests.post('https://api.mailgun.net/v3/lists/indoc@{}/members'.format(domain_url),
            auth=auth,
            data={'subscribed': True,
                  'address': email,
                  'name': name})
email_1 = requests.post('https://api.mailgun.net/v3/{}/messages'.format(domain_url),
            auth=auth,
            data={"from": from_email,
                  "to": '{} <{}>'.format(name, email),
                  "subject": "Welcom to Python Financial",
                  "html": open("templates/email1.html")})
print 'Response status code: ', email_1.status_code
print 'Data: ', email_1.json()
email_2 = requests.post('https://api.mailgun.net/v3/{}/messages'.format(domain_url),
            auth=auth,
            data={"from": from_email,
                  "to": '{} <{}>'.format(name, email),
                  "subject": "Notes and Pizza!",
                  "html": open("templates/email2.html"),
                  "o:deliverytime": time1.strftime("%a, %d %b %Y %H:%M:%S +0000")})
print 'Response status code: ', email_2.status_code
print 'Data: ', email_2.json()
email_3 = requests.post('https://api.mailgun.net/v3/{}/messages'.format(domain_url),
            auth=auth,
            data={"from": from_email,
                  "to": '{} <{}>'.format(name, email),
                  "subject": "Your questions answered",
                  "html": open("templates/email3.html"),
                  "o:deliverytime": time2.strftime("%a, %d %b %Y %H:%M:%S +0000")})
print 'Response status code: ', email_3.status_code
print 'Data: ', email_3.json()
email_4 = requests.post('https://api.mailgun.net/v3/{}/messages'.format(domain_url),
            auth=auth,
            data={"from": from_email,
                  "to": '{} <{}>'.format(name, email),
                  "subject": "Why are we not doing this?",
                  "html": open("templates/email4.html"),
                  "o:deliverytime": time3.strftime("%a, %d %b %Y %H:%M:%S +0000")})
print 'Response status code: ', email_4.status_code
print 'Data: ', email_4.json()

我需要能够将参数传递给托管此函数的url,以便从另一个应用程序传入“name”和“email”变量。调用函数时,名称和电子邮件变量将被格式化为URL,但如何在此函数中接收它们?

2 个答案:

答案 0 :(得分:0)

如果您正在使用API​​网关和aws lambda代理集成,那么无论您将哪些传递给GET作为参数,例如param1 = something&amp; param2 = somethingelse最终会出现在event对象下的字典中,如下所示:

def indoctrination(event, context):
    print(event['queryStringParameters']['param1'])
    print(event['queryStringParameters']['param2'])

答案 1 :(得分:0)

我经过一些试验和错误后想出来了。我只需要将我的姓名和电子邮件变量放在request.args.get()然后from flask import request