简体   繁体   English

如何在 AWS EC2 上运行 python 代码然后使用 POST?

[英]How can I run a python code on AWS EC2 and then use POST?

lets say I have this code假设我有这个代码

s =int(input("Input a number "))

x = s+5 

print(x)

how do I run it on EC2 and then use flask to get the output using POST so I can use it on a front end?如何在 EC2 上运行它,然后使用 flask 使用 POST 获取 output 以便我可以在前端使用它?

I want to make a webpage where a user can input s and then the backend which is an index.py file that communicates with AWS EC2 (using flask) to run the python code above and then return the value x to the front end我想制作一个网页,用户可以在其中输入 s 然后后端是一个 index.py 文件,它与 AWS EC2(使用烧瓶)通信以运行上面的 python 代码,然后将值 x 返回到前端

flask app sample code flask 应用示例代码

#!/usr/bin/python
from flask import Flask, request, jsonify
from index import some_fun

app = Flask(_name_)


@app.route('/foo', methods=['POST'])
def foo():
    data = request.form.to_dict()
    function_response = some_fun(data['form_field'])
    print(function_response)
    return jsonify(data)

if __name__ == "__main__":
    app.run()

You python script, index.py from where you want call some_function你 python 脚本,index.py 你想从哪里调用 some_function

def some_fun(x): return x + 5

here i have considered post request is of multipart/form-data.在这里,我认为发布请求是多部分/表单数据。 if you are making post request with application/json then如果您正在使用 application/json 进行发布请求,那么

data = request.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 AWS (EC2/Lambda) 上运行 python 代码 - How to run python code on AWS (EC2/Lambda) 如何/我可以使用 python 3 设置一个实例以使用已经为 AWS ec2 制作的实例 - How/Can I set up an instance to use an already made for AWS ec2 using python 3 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine? 如何使用本地输入和 output 在 aws ec2 上运行代码 - How to run code on aws ec2 with local input and output 如何在AWS EC2服务器上启动时运行命令(python文件) - How run a command (python file) on boot on AWS EC2 server 我如何运行一个功能更强大的AWS EC2实例,该实例在收到请求时启动和停止? - How can I run a more powerful AWS EC2 instance that starts and stops when I receive a request? 如何运行位于 AWS EC2 服务器上的 python 脚本? - How do I run a python script that is located on the AWS EC2 server? 如何在 AWS EC2 ubuntu 实例中将我的 python3 版本从 3.5.2 升级到 3.7? - How Can I upgrade my python3 version from 3.5.2 to 3.7 in AWS EC2 ubuntu instance? 如何在我的AWS EC2实例上处理多个Python请求? - How can I handle multiple Python requests on my AWS EC2 instance? 如何在Amazon EC2 Web服务上运行python代码? - how to run python code on amazon ec2 webservice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM