我正在尝试在 ec2 实例上部署一个简单的 Flask 应用程序,但无法发送任何 http 请求,我在创建安全组时尝试了 ip 和端口的不同组合,但它仍然无法工作。 该应用程序根据需要在我的本地主机上运行。 这是代码
import logging
import sys
import threading
from flask import Flask, request, jsonify
from main import mainfunction
import concurrent.futures
# Configure logging to capture output
app = Flask(__name__)
# Your asynchronous function that will be called
def process_client(client):
# Redirect stdout to the console
sys.stdout = sys.__stdout__
# Create a thread to run mainfunction in the background
thread = threading.Thread(target=mainfunction, args=(client,))
thread.start()
return "Processing client in the background..."
@app.route('/process', methods=['POST'])
def process():
try:
data = request.get_json()
if 'client' in data:
client = data['client']
# Call the asynchronous function using asyncio
result = process_client(client)
return jsonify({'result': result})
else:
return jsonify({'error': 'Missing client data in request'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/', methods=['GET'])
def status():
return 'Server is listening'
if __name__ == '__main__':
app.run(debug=True)
这是我设置的入站和出站规则
需要确保网络服务器正在响应。您可能会使用 cmd 提示符和 localhost url 进行检查。似乎不是安全组/防火墙问题。要确认这一点,您可以临时允许入站和出站 http+https+SSH 请求的“所有流量”。如果这没有帮助,您可以通过 WhatsApp +917028020321 联系我。