Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created March 27, 2025 05:47
Show Gist options
  • Save gregtemp/4ee2256240bf77a3aca5100a70597067 to your computer and use it in GitHub Desktop.
Save gregtemp/4ee2256240bf77a3aca5100a70597067 to your computer and use it in GitHub Desktop.
# a light Pastebin for your lan # -anyone can read/write # -deletes file when shut down
#!/usr/bin/env python3
import os
import sys
import tempfile
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib.parse
import socket
# a light Pastebin for your lan
# -anyone can read/write
# -deletes file when shut down
class PastebinHandler(BaseHTTPRequestHandler):
# Class variable to store the file path
file_path = None
def do_GET(self):
# Simple HTML page with a text area and save button
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
# Read current file contents
file_contents = ""
try:
with open(self.file_path, 'r') as f:
file_contents = f.read()
except FileNotFoundError:
pass
# Very basic HTML with no JavaScript
html = f'''
<!DOCTYPE html>
<html>
<head>
<title>LAN Pastebin</title>
<style>
body {{ font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }}
textarea {{ width: 100%; height: 300px; margin-bottom: 10px; }}
</style>
</head>
<body>
<h1>LAN Pastebin</h1>
<form method="POST" action="/">
<textarea name="content" placeholder="Enter your text here">{file_contents}</textarea>
<input type="submit" value="Save">
</form>
</body>
</html>
'''
self.wfile.write(html.encode())
def do_POST(self):
# Handle saving content
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
decoded_data = urllib.parse.parse_qs(post_data.decode('utf-8'))
# Save content to file
if 'content' in decoded_data:
content = decoded_data['content'][0]
with open(self.file_path, 'w') as f:
f.write(content)
# Redirect back to main page
self.send_response(303)
self.send_header('Location', '/')
self.end_headers()
def get_local_ip():
"""Get the local IP address of the machine."""
try:
# Create a temporary socket to determine local IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
return local_ip
except Exception:
return 'localhost'
def start_server(port=8000):
# Create a temporary file
temp_file = tempfile.mktemp(suffix='.txt', prefix='lan_pastebin_')
# Set the file path for the handler
PastebinHandler.file_path = temp_file
# Start the server
server_address = ('', port)
httpd = HTTPServer(server_address, PastebinHandler)
local_ip = get_local_ip()
print(f"Serving at http://{local_ip}:{port}")
print(f"Temporary file: {temp_file}")
print("Press Ctrl+C to stop the server and delete the file.")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
httpd.server_close()
# Clean up the temporary file
try:
os.remove(temp_file)
print(f"Temporary file {temp_file} deleted.")
except Exception as e:
print(f"Error deleting file: {e}")
def main():
# Allow optional port specification
port = 8000
if len(sys.argv) > 1:
try:
port = int(sys.argv[1])
except ValueError:
print("Invalid port number. Using default 8000.")
start_server(port)
# This allows running as a module
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment