Skip to content

Commit a0b4ba8

Browse files
committed
fri added
1 parent 1bdb673 commit a0b4ba8

6 files changed

Lines changed: 90 additions & 0 deletions

File tree

fri/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM centos/python-36-centos7
2+
3+
WORKDIR /fri
4+
COPY . /fri
5+
6+
COPY requirements.txt /fri/requirements.txt
7+
RUN pip install -r /fri/requirements.txt
8+
9+
10+
CMD ["gunicorn", "-w", "4", "--bind", "0.0.0.0:8080", "main:app"]

fri/example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("amit")

fri/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from flask import Flask, request, jsonify, render_template
2+
from werkzeug.utils import secure_filename
3+
import os
4+
5+
6+
UPLOAD_FOLDER = "./uploaded_files"
7+
8+
if not os.path.exists(UPLOAD_FOLDER):
9+
os.makedirs(UPLOAD_FOLDER)
10+
11+
app = Flask(__name__)
12+
app.secret_key = "secret key"
13+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
14+
15+
@app.route('/')
16+
def hello_world():
17+
return render_template('home.html')
18+
19+
@app.route('/multiple-files-upload', methods=['POST'])
20+
def upload_file():
21+
if 'files[]' not in request.files:
22+
resp = jsonify({'message' : 'No file in the request'})
23+
resp.status_code = 400
24+
return resp
25+
26+
files = request.files.getlist('files[]')
27+
28+
errors = {}
29+
success = False
30+
31+
for file in files:
32+
if file:
33+
filename = secure_filename(file.filename)
34+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
35+
success = True
36+
37+
if success and errors:
38+
errors['message'] = 'File(s) successfully uploaded'
39+
resp = jsonify(errors)
40+
resp.status_code = 500
41+
return resp
42+
if success:
43+
resp = jsonify({'message' : 'Files successfully uploaded'})
44+
resp.status_code = 201
45+
return resp
46+
else:
47+
resp = jsonify(errors)
48+
resp.status_code = 500
49+
return resp
50+
51+
if __name__ == "__main__":
52+
app.run(host="0.0.0.0")

fri/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Flask
2+
gunicorn==20.1.0

fri/templates/home.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<h1>Home page</h1>
11+
</body>
12+
</html>

fri/test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import requests
2+
3+
url = "http://127.0.0.1:5000/multiple-files-upload"
4+
5+
payload={}
6+
files=[
7+
('files[]',('example.py',open('/home/amit/Desktop/concore/flaskApi/example.py','rb'),'application/octet-stream'))
8+
]
9+
headers = {}
10+
11+
response = requests.request("POST", url, headers=headers, data=payload, files=files)
12+
13+
print(response.text)

0 commit comments

Comments
 (0)