Certainly! Below is a Python application using the Flask framework that will allow all devices connected to the same network to share messages and files through a web interface. Users can access this interface by navigating to the hosted IP address of the machine running the app.
Features of the App:
- Message Sharing: Users can post and view messages on a shared message board.
- File Sharing: Users can upload files that other family members can download.
- Web Interface: Accessible via a web browser from any device on the same network.
Requirements:
- Python 3.x installed on the hosting machine.
- Flask library installed (
pip install flask).
Step-by-Step Guide
1. Install Flask
First, ensure that Flask is installed. Open your terminal or command prompt and run:
pip install flask
2. Create the Application File
Create a file named app.py and add the following code:
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import os
from werkzeug.utils import secure_filename
app = Flask(__name__)
# Configuration
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Allowed extensions for file uploads
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'zip', 'rar', 'docx', 'xlsx', 'pptx'])
# In-memory storage for messages (will reset on server restart)
messages = []
def allowed_file(filename):
"""Check if the file has one of the allowed extensions."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Handle message submission
message = request.form.get('message')
if message:
messages.append(message)
# Handle file upload
if 'file' in request.files:
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('index'))
# Retrieve the list of uploaded files
files = os.listdir(app.config['UPLOAD_FOLDER'])
return render_template('index.html', messages=messages, files=files)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
"""Serve uploaded files."""
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == '__main__':
# Run the app on all network interfaces on port 5000
app.run(host='0.0.0.0', port=5000)
3. Create the Templates Directory and HTML File
Create a directory named templates in the same folder as app.py. Inside the templates directory, create a file named index.html and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Family Shared Space</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1, h2 { color: #333; }
textarea { width: 100%; }
ul { list-style-type: none; padding: 0; }
li { background: #f4f4f4; margin: 5px 0; padding: 10px; }
.message-form, .file-form { margin-bottom: 30px; }
.messages, .files { margin-bottom: 30px; }
.file-link { text-decoration: none; color: #0066cc; }
.file-link:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>Welcome to the Family Shared Space</h1>
<!-- Message Form -->
<div class="message-form">
<h2>Share a Message</h2>
<form method="POST">
<textarea name="message" rows="4" placeholder="Enter your message here..."></textarea><br>
<input type="submit" value="Post Message">
</form>
</div>
<!-- Display Messages -->
<div class="messages">
<h2>Messages</h2>
{% if messages %}
<ul>
{% for msg in messages %}
<li>{{ msg }}</li>
{% endfor %}
</ul>
{% else %}
<p>No messages yet.</p>
{% endif %}
</div>
<!-- File Upload Form -->
<div class="file-form">
<h2>Upload a File</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload File">
</form>
</div>
<!-- Display Files -->
<div class="files">
<h2>Files</h2>
{% if files %}
<ul>
{% for file in files %}
<li>
<a class="file-link" href="{{ url_for('uploaded_file', filename=file) }}">{{ file }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No files uploaded yet.</p>
{% endif %}
</div>
</body>
</html>
4. Run the Application
In your terminal or command prompt, navigate to the directory containing app.py and run:
python app.py
You should see output indicating that the server is running, such as:
* Serving Flask app 'app'
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
5. Access the Web Interface
From any device connected to the same network:
-
Find the local IP address of the machine running the app. You can do this by running ipconfig (Windows) or ifconfig (Linux/Mac) in the terminal and looking for the IP address associated with your network adapter (e.g., 192.168.1.100).
-
Open a web browser and navigate to:
http://<HOST_IP>:5000/
Replace <HOST_IP> with the IP address of the host machine.
How the Application Works
-
Message Sharing:
- Users can submit messages using the form.
- Messages are stored in the
messages list and displayed to all users.
- Note: Messages are stored in memory and will be lost if the server restarts.
-
File Sharing:
- Users can upload files through the file upload form.
- Files are saved in the
uploads directory on the server.
- Uploaded files are listed on the page with links to download them.
-
Web Interface:
- The
index.html template displays the forms and lists of messages and files.
- The
render_template function passes the messages and files variables to the template.
Security Considerations
Possible Enhancements
-
Add Authentication:
- Implement a simple login system to restrict access to authorized family members.
-
Persistent Storage:
- Use a database to store messages and file information persistently.
-
Better Styling:
- Enhance the HTML templates with CSS frameworks like Bootstrap for a more polished look.
-
Use HTTPS:
- Serve the app over HTTPS to encrypt data in transit (requires additional setup).
Conclusion
This simple Flask application provides a shared space for family members on the same network to exchange messages and files easily. By accessing the hosted IP address, any family member can contribute and benefit from this shared resource.
Feel free to customize and enhance the app to better suit your family's needs! If you have any questions or need further assistance, please let me know.