如何使用python在AWS Lambda中进行HTTP REST调用?

编程入门 行业动态 更新时间:2024-10-11 15:19:17
本文介绍了如何使用python在AWS Lambda中进行HTTP REST调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

要使用python进行http调用,我的方法是使用 requests .

To make an http call using python my way was to use requests.

但是 requests 并未安装在lambda上下文中.使用导入请求导致未找到模块错误.

But requests is not installed in lambda context. Using import requests resulted in module is not found error.

另一种方法是使用来自botocore.vendored导入请求的提供的库 .但是AWS不推荐使用此库.

The other way is to use the provided lib from botocore.vendored import requests. But this lib is deprecated by AWS.

我想避免将依赖项打包在我的lambda zip文件中.

I want to avoid to package dependencies within my lambda zip file.

在基于python的lambda中进行REST调用的最聪明的解决方案是什么?

What is the smartest solution to make a REST call in python based lambda?

推荐答案

解决方案1)

由于不赞成使用botocore.vendored导入请求中的 ,因此推荐的方法是安装依赖项.

Since from botocore.vendored import requests is deprecated the recomended way is to install your dependencies.

$ pip install requests

import requests response = requests.get('...')

另请参阅. aws.amazon/de/blogs/developer/remove-the-vendored-version-of-requests-from-botocore/

但是您必须注意将依赖项打包到lambda zip中.

But you have to take care for packaging the dependencies within your lambda zip.

解决方案2)

我的首选解决方案是使用 urllib .它在您的lambda执行上下文中.

My preferred solution is to use urllib. It's within your lambda execution context.

repl.it/@SmaMa/DutifulChocolateApplicationprogrammer

import urllib.request import json res = urllib.request.urlopen(urllib.request.Request( url='asdfast.beobit/api/', headers={'Accept': 'application/json'}, method='GET'), timeout=5) print(res.status) print(res.reason) print(json.loads(res.read()))

解决方案3)

使用 http.client ,它也在lambda执行上下文中.

Using http.client, it's also within your lambda execution context.

repl.it/@SmaMa/ExoticUnsightlyAstrophysics

import http.client connection = http.client.HTTPSConnection('fakerestapi.azurewebsites') connection.request('GET', '/api/Books') response = connection.getresponse() print(response.read().decode())

更多推荐

如何使用python在AWS Lambda中进行HTTP REST调用?

本文发布于:2023-11-28 02:54:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   python   AWS   REST   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!