【发布时间】:2019-11-09 20:28:32
【问题描述】:
我正在尝试通过发送带有 IP 的 GET 请求来查询返回 IP 地理位置信息的开源 API。
我正在使用包含 IP 地址(位于key1)的密钥测试代码。我正在尝试在请求发送后获取信息,但我不确定我做错了什么。
我尝试将 IP 附加到 url 的末尾(如 geoip API 所示),但我不断收到语法错误。
import json
from botocore.vendored import requests
def lambda_handler(resp, requests, event):
event = event.key1
url = "https://freegeoip.app/json/" +event
headers = {
'accept': "application/json",
'content-type': "application/json"
}
response = requests.request("GET", url, headers=headers)
print(response.text)
我的代码在下面的常规 python 语法中工作,只是不知道如何让它与 lambda 一起工作
import requests
userIP = '54.81.183.174'
def theFunction():
url = "https://freegeoip.app/json/" + userIP
headers = {
'accept': "application/json",
'content-type': "application/json"
}
response = requests.request("GET", url, headers=headers)
print(response.text)
theFunction()
【问题讨论】:
标签: python function aws-lambda