0% found this document useful (0 votes)
6 views3 pages

Flask Payload Demo with SQLite

This document provides the complete source code and setup instructions for a payload demo application using Python, Flask, SQLite, and file uploads. It includes an HTML frontend for building and sending payloads, a Flask backend for handling requests and storing data, and a SQLite database for persistence. The README outlines the steps to set up the project, including creating a virtual environment and running the server.

Uploaded by

kelvinose32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Flask Payload Demo with SQLite

This document provides the complete source code and setup instructions for a payload demo application using Python, Flask, SQLite, and file uploads. It includes an HTML frontend for building and sending payloads, a Flask backend for handling requests and storing data, and a SQLite database for persistence. The README outlines the steps to set up the project, including creating a virtual environment and running the server.

Uploaded by

kelvinose32
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Full Payload Demo (Python + Flask + SQLite + File

Uploads)

This document contains the full source code and setup instructions for a safe, educational payload
demo. It uses Flask as the backend, SQLite for storage, and a simple HTML/JavaScript frontend for
building and sending payloads.

1. public/[Link]
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Full Payload Demo (Flask + SQLite + Uploads)</title>
<style>
body{font-family:system-ui,-apple-system,'Segoe UI',Roboto,Arial;background:#f8fafc;color:#0b1220
.card{max-width:1000px;margin:0 auto;background:white;padding:18px;border-radius:10px;box-shadow:
label{display:block;margin-top:10px;font-weight:600}
input,textarea,select,button{width:100%;padding:10px;border-radius:8px;border:1px solid #e6edf3}
textarea{min-height:120px;resize:vertical}
.controls{display:flex;gap:8px;margin-top:12px}
.preview{background:#0f1724;color:#e6f7ff;padding:10px;border-radius:6px;font-family:monospace;wh
</style>
</head>
<body>
<div class="card">
<h2>Full Payload Demo</h2>
<label>Title</label>
<input id="title" />
<label>Description</label>
<textarea id="description"></textarea>
<label>Type</label>
<select id="type"><option>note</option><option>task</option><option>report</option></select>
<label>Priority</label>
<select id="priority"><option>low</option><option selected>normal</option><option>high</option></
<label>Attachments</label>
<input id="files" type="file" multiple />
<div class="controls">
<button id="build">Build Preview</button>
<button id="send">Send Payload</button>
<button id="view">View Stored</button>
</div>
<pre id="preview">{}</pre>
<pre id="stored">(not loaded)</pre>
</div>
<script>
// JS code omitted for brevity
</script>
</body>
</html>

2. [Link]
import sqlite3, os, json, datetime, uuid
from pathlib import Path
from flask import Flask, request, jsonify, send_from_directory, g, abort

BASE = Path(__file__).[Link]()
UPLOADS = BASE / 'uploads'
DBFILE = BASE / '[Link]'
[Link](exist_ok=True)
app = Flask(__name__, static_folder='public')
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = [Link](str(DBFILE))
db.row_factory = [Link]
return db

def init_db():
db = get_db()
[Link]('''CREATE TABLE IF NOT EXISTS payloads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TEXT NOT NULL,
title TEXT,
description TEXT,
type TEXT,
priority TEXT,
attachments TEXT
)''')
[Link]()

@app.before_first_request
def setup():
init_db()

@[Link]('/api/payloads', methods=['POST'])
def receive_payload():
title = [Link]('title')
description = [Link]('description')
if not title or not description or len(description) < 10:
return jsonify({'error': 'Invalid input'}), 400

files_info = []
for f in [Link]('files'):
if [Link]:
ext = [Link]([Link])[1]
fname = f"{uuid.uuid4().hex}{ext}"
[Link](UPLOADS / fname)
files_info.append({'orig': [Link], 'stored': fname})

ts = [Link]().isoformat() + 'Z'
db = get_db()
cur = [Link]('INSERT INTO payloads (ts, title, description, type, priority, attachments) VALU
(ts, title, description, [Link]('type'), [Link]('priority'),
[Link]()
return jsonify({'status': 'ok', 'id': [Link], 'ts': ts})

@[Link]('/api/payloads')
def list_payloads():
db = get_db()
rows = [Link]('SELECT * FROM payloads ORDER BY id DESC').fetchall()
return jsonify({'count': len(rows), 'items': [dict(r) for r in rows]})

@[Link]('/')
def root():
return send_from_directory('public', '[Link]')

if __name__ == '__main__':
[Link](debug=True, port=5000)

3. [Link]
Flask==2.3.2

4. [Link]
Setup Instructions:

1. Unzip or create the project folder with the files listed above.
2. Open a terminal in that folder.
3. Create a Python virtual environment:
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\Activate.ps1 # Windows PowerShell
4. Install dependencies:
pip install -r [Link]
5. Run the server:
python [Link]
6. Open browser to:
[Link]

You might also like