Upload 3 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
|
| 3 |
+
app = Flask(__name__)
|
| 4 |
+
|
| 5 |
+
# File path
|
| 6 |
+
FILE_PATH = 'data.txt'
|
| 7 |
+
|
| 8 |
+
@app.route('/file', methods=['GET', 'POST'])
|
| 9 |
+
def handle_file():
|
| 10 |
+
if request.method == 'GET':
|
| 11 |
+
# Read operation
|
| 12 |
+
try:
|
| 13 |
+
with open(FILE_PATH, 'r') as file:
|
| 14 |
+
content = file.read()
|
| 15 |
+
return jsonify({
|
| 16 |
+
'status': 'success',
|
| 17 |
+
'data': content
|
| 18 |
+
}), 200
|
| 19 |
+
except FileNotFoundError:
|
| 20 |
+
return jsonify({
|
| 21 |
+
'status': 'error',
|
| 22 |
+
'message': 'File not found'
|
| 23 |
+
}), 404
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return jsonify({
|
| 26 |
+
'status': 'error',
|
| 27 |
+
'message': str(e)
|
| 28 |
+
}), 500
|
| 29 |
+
|
| 30 |
+
elif request.method == 'POST':
|
| 31 |
+
# Write operation
|
| 32 |
+
try:
|
| 33 |
+
# Get the content from request
|
| 34 |
+
content = request.json.get('content')
|
| 35 |
+
|
| 36 |
+
if not content:
|
| 37 |
+
return jsonify({
|
| 38 |
+
'status': 'error',
|
| 39 |
+
'message': 'No content provided'
|
| 40 |
+
}), 400
|
| 41 |
+
|
| 42 |
+
# Write mode can be specified in the request
|
| 43 |
+
mode = request.json.get('mode', 'w') # Default to 'w' (overwrite)
|
| 44 |
+
if mode not in ['w', 'a']: # Only allow write or append modes
|
| 45 |
+
mode = 'w'
|
| 46 |
+
|
| 47 |
+
with open(FILE_PATH, mode) as file:
|
| 48 |
+
file.write(content)
|
| 49 |
+
|
| 50 |
+
return jsonify({
|
| 51 |
+
'status': 'success',
|
| 52 |
+
'message': f'Content {"appended" if mode == "a" else "written"} successfully'
|
| 53 |
+
}), 200
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return jsonify({
|
| 57 |
+
'status': 'error',
|
| 58 |
+
'message': str(e)
|
| 59 |
+
}), 500
|
| 60 |
+
|
| 61 |
+
if __name__ == '__main__':
|
| 62 |
+
app.run(debug=True)
|
data.txt
ADDED
|
File without changes
|
usage.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
# Read from file
|
| 4 |
+
response = requests.get('http://localhost:5000/file')
|
| 5 |
+
print(response.json())
|
| 6 |
+
|
| 7 |
+
# Write to file (overwrite)
|
| 8 |
+
response = requests.post('http://localhost:5000/file',
|
| 9 |
+
json={'content': 'Hello, World!'})
|
| 10 |
+
print(response.json())
|
| 11 |
+
|
| 12 |
+
# Append to file
|
| 13 |
+
response = requests.post('http://localhost:5000/file',
|
| 14 |
+
json={'content': '\nNew line', 'mode': 'a'})
|
| 15 |
+
print(response.json())
|