AWS Lambda的python中的请求模块问题

时间:2016-06-28 16:16:16

标签: python amazon-web-services aws-lambda

我正在编写一个lambda函数,其意图使用从 Wolfram CloudObject中提取信息的请求。以下是代码的相关部分:

from __future__ import print_function
import requests

.
.
.

def on_intent(intent_request, session):
    print("on_intent requestID=" + intent_request['requestID'] + ", sessionID=" + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to skill's intent handlers

    if intent_name == "GetEvent":
         return call_wolfram(intent, session)
    elif intent_name == "AMAZON.HelpIntent":
         return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
         return handle_session_end_request()
    else:
         raise ValueError("Invalid intent")

.
.
.

# Functions that control skill's behavior

def call_wolfram(intent, session):
    url = "https://path-to-cloud-object"
    query = {'string1': 'VESSEL', 'string2': 'EVENT', 'RelString': 'TRIGGERED'}
    r = requests.get(url, params=query)
    session_attributes = {"r_result": r}
    speech_output = "Congrats, dummy! It worked"
    card_title = "Query"
    should_end_session = True
    return build_response({}, build_speechlet_response(card_title, speech_output, None, should_end_session)

其余大部分代码都遵循AWS提供的MyColorIs示例模板,只做了很少的更改。测试lambda函数时,错误消息为我提供了一个带有stackTrace的json文件;我已将问题缩小到代码行r = requests.get()session_attributes = {},因为当注释掉时,lambda执行成功。这是我的第一个使用python的项目,所以我也是该语言的新手。为了更好地衡量,这是lambda执行后得到的错误消息:

{
  "stackTrace": [
   [
     "/var/task/query_lambda.py",
     27,
     "lambda_handler",
     "return on_intent(event['request'], event['session'])"
   ],
   [
     "/var/task/query_lambda.py",
     65,
     "on_intent",
     "return call_wolfram(intent, session)"
   ],
   [
     "/var/task/query_lambda.py",
     113,
     "call_wolfram",
     "r = requests.get(url, params=query)"
   ],
   [
     "/var/task/requests/api.py",
     71,
     "get",
     "return request('get', url, params=params, **kwargs)"
   ],
   [
     "/var/task/requests/api.py",
     57,
     "request",
     "return session.request(method=method, url=url, **kwargs)"
   ],
   [
     "/var/task/requests/sessions.py",
     475,
     "request",
     "resp = self.send(prep, **send_kwargs)"
   ],
   [
     "/var/task/requests/sessions.py",
     585,
     "send",
     "r = adapter.send(request, **kwargs)"
   ],
   [
     "/var/task/requests/adapters.py",
     477,
     "send",
     "raise SSLError(e, request=request)"
    ]
  ],
    "errorType": "SSLError",
    "errorMessage": "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify    failed (_ssl.c:590)"
}

2 个答案:

答案 0 :(得分:5)

lambda环境不包含certifi模块,除非您使用您的函数上传它。

我会这样做:

cd <directory with your lambda>
pip install certifi -t .
zip ../lambda *

然后将../lambda.zip上传到亚马逊。

答案 1 :(得分:1)

您可以阅读有关请求的更多信息。在这里使用证书:

http://docs.python-requests.org/en/master/user/advanced/

有两种方法可以解决这个问题:

  • 找到您缺少的证书,并将其安装在失败的系统上。
  • verify=False传递给requests.get,完全忽略证书:

    r = requests.get(url, params=query, verify=False)

第二种方法更快,但不太安全;对您的预期用途可能或不重要。