Skip to content

Instantly share code, notes, and snippets.

@pszemraj
Last active March 13, 2024 00:20
Show Gist options
  • Save pszemraj/388269fdfb1f1355f5bfa2f816ce1253 to your computer and use it in GitHub Desktop.
Save pszemraj/388269fdfb1f1355f5bfa2f816ce1253 to your computer and use it in GitHub Desktop.
local network pastebin server for copy/pasting betwixt 2+ computers
import json
import socket
import uuid
import yake
from flask import Flask, redirect, render_template_string, request, url_for
from markupsafe import escape
app = Flask(__name__)
# Attempt to load pastes from file
try:
with open("pastes.json", "r", encoding="utf-8") as f:
pastes = json.load(f)
except FileNotFoundError:
pastes = {}
def save_pastes():
try:
with open("pastes.json", "w", encoding="utf-8") as f:
json.dump(pastes, f)
except IOError:
print("Error saving pastes to file.")
def generate_title(content):
kw_extractor = yake.KeywordExtractor()
keywords = kw_extractor.extract_keywords(content)
return ", ".join([kw[0] for kw in keywords[:3]]) # Use top 3 keywords as the title
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
paste_content = request.form["content"]
paste_id = str(uuid.uuid4())
title = request.form.get("title", "").strip()
if not title:
title = generate_title(paste_content)
pastes[paste_id] = {"content": paste_content, "title": title}
save_pastes()
return redirect(url_for("home"))
# Display existing pastes as copyable text areas with titles and delete buttons
paste_blocks = "\n".join(
[
f'<div><h3>{escape(paste["title"])}</h3><textarea readonly rows="10" cols="30">{escape(paste["content"])}</textarea><br><a href="{url_for("delete_paste", paste_id=paste_id)}">Delete</a></div><br>'
for paste_id, paste in list(pastes.items())[
:10
] # Limit to 10 most recent pastes
]
)
return f"""
<html>
<head>
<title>Local Pastebin</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
}}
textarea {{
width: 400px;
height: 200px;
}}
</style>
</head>
<body>
<h2>Submit a new Paste</h2>
<form method="post">
<label for="title">Title (optional):</label><br>
<input type="text" id="title" name="title"><br>
<label for="content">Content:</label><br>
<textarea name="content" rows="10" cols="30" required></textarea><br>
<input type="submit" value="Save">
</form>
<h2>Recent Pastes</h2>
{paste_blocks if paste_blocks else 'No pastes yet.'}
</body>
</html>
"""
@app.route("/delete/<paste_id>")
def delete_paste(paste_id):
if paste_id in pastes:
del pastes[paste_id]
save_pastes()
return redirect(url_for("home"))
def get_host_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("10.255.255.255", 1))
IP = s.getsockname()[0]
except Exception:
IP = "127.0.0.1"
finally:
s.close()
return IP
if __name__ == "__main__":
host_ip = get_host_ip()
print(f"Server starting. Navigate to http://{host_ip}:5000 on your local network.")
app.run(host="0.0.0.0", port=5000, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment