Загрузка данных
import os
import socket
from flask import Flask, request, send_from_directory, render_template_string
import qrcode
app = Flask(__name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Передача на ПК</title>
<style>
body { font-family: sans-serif; padding: 20px; max-width: 600px; margin: 0 auto; background: #f4f4f9; }
.card { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 20px; }
textarea, input[type="file"], input[type="submit"] { width: 100%; margin-top: 10px; box-sizing: border-box; }
textarea { height: 120px; }
input[type="submit"] { padding: 12px; background: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
ul { list-style: none; padding: 0; }
li { margin: 8px 0; }
a { color: #007bff; text-decoration: none; }
</style>
</head>
<body>
<div class="card">
<h3>Отправить текст на ПК</h3>
<form action="/send_text" method="post">
<textarea name="text" placeholder="Введите или вставьте текст..."></textarea>
<input type="submit" value="Сохранить в enter.txt">
</form>
</div>
<div class="card">
<h3>Отправить файл на ПК</h3>
<form action="/upload_file" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Загрузить файл">
</form>
</div>
<div class="card">
<h3>Файлы на сервере</h3>
<ul>
{% for file in files %}
{% if file not in ['share.py', 'gui.py', 'enter.txt'] %}
<li><a href="/download/{{ file }}" download>{{ file }}</a></li>
{% endif %}
{% endfor %}
</ul>
</div>
</body>
</html>
"""
def get_local_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
@app.route('/')
def index():
files = [f for f in os.listdir(BASE_DIR) if os.path.isfile(os.path.join(BASE_DIR, f))]
return render_template_string(HTML_TEMPLATE, files=files)
@app.route('/send_text', methods=['POST'])
def receive_text():
text = request.form.get('text', '')
if text:
text = text.replace('\r\n', '\n').replace('\r', '\n').strip()
text_for_notepad = text.replace('\n', '\r\n')
filepath = os.path.join(BASE_DIR, 'enter.txt')
with open(filepath, 'a', encoding='utf-8', newline='') as f:
f.write(text_for_notepad + '\r\n---\r\n')
return index()
@app.route('/upload_file', methods=['POST'])
def receive_file():
if 'file' in request.files:
file = request.files['file']
if file.filename != '':
file.save(os.path.join(BASE_DIR, file.filename))
return index()
@app.route('/download/<path:filename>')
def download_file(filename):
return send_from_directory(BASE_DIR, filename, as_attachment=True)
if __name__ == '__main__':
port = 5000
ip = get_local_ip()
url = f"http://{ip}:{port}"
qr = qrcode.QRCode()
qr.add_data(url)
qr.make(fit=True)
print(f"\nСервер запущен. Адрес: {url}\n")
qr.print_ascii(invert=True)
app.run(host='0.0.0.0', port=port)