[英]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.