backendFlask.py
python_old/backendFlask.py
from flask import Flask, request, jsonify
app = Flask(__name__)
CORS(app) # Enable CORS for frontend access
@app.route('/', methods=['GET'])
def index():
return "Hello, World!"
@app.route('/api/userinfo', methods=['GET'])
def get_user_info():
## Interaction with database
## ....
## ....
return jsonify({
"name": "Nelson Lai",
"age": 42,
"email": "nelson@example.com"
})
# POST: Create user
@app.route('/api/userinfo', methods=['POST'])
def create_user():
headers = request.headers
data = request.get_json()
name = data['name']
password = data['password']
## perform database query
# Normally, you'd save this to a database
return jsonify({"message": "User created", "user": data}), 201
# PUT: Update user by ID
@app.route('/api/userinfo/<int:user_id>', methods=['PUT'])
def update_user(user_id):
userId = user_id
SQL ... (userId)
data = request.get_json()
# Normally, you'd update the user in your database here
return jsonify({"message": f"User {user_id} updated", "user": data})
if __name__ == '__main__':
app.run(port=5000)
相關文章
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
閱讀文章 →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
閱讀文章 →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
閱讀文章 →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
閱讀文章 →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
閱讀文章 →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
閱讀文章 →