server.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. import os
  3. from flask import Flask, request, jsonify
  4. from werkzeug.utils import secure_filename
  5. from flask_cors import CORS
  6. UPLOAD_FOLDER = 'uploads'
  7. ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  8. app = Flask(__name__)
  9. app.config['UPLOAD_FOLDER'] = os.path.join(os.path.dirname(__file__), UPLOAD_FOLDER)
  10. CORS(app)
  11. def allowed_file(filename):
  12. return '.' in filename and \
  13. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  14. @app.route('/upload', methods=['POST'])
  15. def upload_file():
  16. if request.method == 'POST':
  17. # check if the post request has the file part
  18. print (request.files)
  19. if len(request.files) == 0:
  20. return jsonify(
  21. error="No file n request"
  22. ), 400
  23. for fi in request.files:
  24. file = request.files[fi]
  25. if file and allowed_file(file.filename):
  26. filename = secure_filename(file.filename)
  27. file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  28. return jsonify(
  29. message="ok"
  30. ), 201
  31. if __name__ == '__main__':
  32. app.run(port=3020)