Pregunta de entrevista de Accenture

Flask code for sending data

Respuesta de la entrevista

Anónimo

24 de nov de 2025

from flask import Flask, request, jsonify import csv import os app = Flask(__name__) # -------------------------------------- # Ensure CSV exists # -------------------------------------- CSV_FILE = "data_store.csv" if not os.path.exists(CSV_FILE): with open(CSV_FILE, "w", newline="") as f: writer = csv.writer(f) writer.writerow(["name", "email", "message"]) # -------------------------------------- # Endpoint to receive data (JSON or Form) # -------------------------------------- @app.route('/submit', methods=['POST']) def submit(): try: # 1️⃣ Check if data is sent as JSON if request.is_json: data = request.get_json() name = data.get("name") email = data.get("email") message = data.get("message") else: # 2️⃣ Otherwise treat it as Form data name = request.form.get("name") email = request.form.get("email") message = request.form.get("message") # -------------------------------------- # Basic validation # -------------------------------------- if not name or not email or not message: return jsonify({ "status": "error", "msg": "Missing required fields" }), 400 # -------------------------------------- # Save into CSV file # -------------------------------------- with open(CSV_FILE, "a", newline="") as f: writer = csv.writer(f) writer.writerow([name, email, message]) # -------------------------------------- # Return Response # -------------------------------------- return jsonify({ "status": "success", "msg": "Data received and stored" }), 201 except Exception as e: return jsonify({"status": "error", "msg": str(e)}), 500 # -------------------------------------- # Simple home page # -------------------------------------- @app.route('/') def home(): return "Flask Data Receiver is Running" if __name__ == '__main__': app.run(debug=True

1