Rumah >pembangunan bahagian belakang >Tutorial Python >Permintaan dan Keselamatan HTTP Python: Melindungi Aplikasi Web Anda daripada Serangan
Permintaan Http adalah sebahagian daripada aplikasi WEB, yang membolehkan pelanggan bertukar data dengan pelayan dan melaksanakan pelbagai operasi. Keselamatan adalah salah satu keperluan asas bagi aplikasi Rangkaian. Dalam python, terdapat banyak cara untuk melindungi aplikasi web daripada serangan.
import ssl context = ssl.SSLContext() context.load_cert_chain("server.crt", "server.key") server = http.server.HTTPServer(("", 443), HTTPSHandler) server.Socket = context.wrap_socket(server.socket, server_side=True) server.serve_forever()
from flask.ext.csrf import CSRFProtect csrf = CSRFProtect() csrf.init_app(app)
from flask.ext.xssfilter import XSSFProtect xss = XSSFProtect() xss.init_app(app)
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(app) @app.route("/") def index(): users = db.session.execute("SELECT * FROM users") return render_template("index.html", users=users)
from flask import request, send_from_directory @app.route("/uploads/<path:filename>") def uploaded_file(filename): return send_from_directory("uploads", filename) @app.route("/upload", methods=["POST"]) def upload_file(): file = request.files["file"] if file and file.filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS: file.save(os.path.join(app.config["UPLOAD_FOLDER"], file.filename)) return redirect(url_for("uploaded_file", filename=file.filename)) else: return "Invalid file type."
from flask import Flask, request app = Flask(__name__) @app.route("/") def index(): return "Hello, World!" @app.route("/slow") def slow(): time.sleep(10) return "Slow page" if __name__ == "__main__": app.run(host="0.0.0.0", port=80)
import logging logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) handler = logging.FileHandler("app.log") handler.setLevel(logging.DEBUG) fORMatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) logger.debug("This is a debug message") logger.info("This is an info message") logger.warning("This is a warning message") logger.error("This is an error message") logger.critical("This is a critical message")
Atas ialah kandungan terperinci Permintaan dan Keselamatan HTTP Python: Melindungi Aplikasi Web Anda daripada Serangan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!