如何在AWS EC2上运行python代码,然后使用POST? [英] How can I run a python code on AWS EC2 and then use POST?
问题描述
让我说我有这个密码
s =int(input("Input a number "))
x = s+5
print(x)
如何在EC2上运行它,然后使用flask通过POST获取输出,以便可以在前端使用它?
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 ?
我想制作一个网页,用户可以在其中输入s,然后在后端输入一个index.py文件,该文件与AWS EC2通信(使用flask)以运行上面的python代码,然后将值x返回前端结束
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
推荐答案
烧瓶应用程序示例代码
#!/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()
您想要从其中调用some_function的python脚本index.py
You python script, index.py from where you want call some_function
def some_fun(x):返回x + 5
在这里,我考虑到过帐请求是由多部分/表单数据组成的.如果您正在使用application/json进行发布请求然后
here i have considered post request is of multipart/form-data. if you are making post request with application/json then
data = request.json
这篇关于如何在AWS EC2上运行python代码,然后使用POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!