File size: 2,947 Bytes
109bade 3f2b61a af56c3a 109bade cb037b0 3f2b61a 109bade 3f2b61a ebdd193 109bade 4cd23ec 109bade 3f2b61a 109bade 3f2b61a 109bade af56c3a 77dd2bf 9c23a1d 598cf95 77dd2bf 67f4f3e 77dd2bf ed8c51f 109bade af56c3a 109bade af56c3a f18f04b 109bade |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
from flask import Flask, request, send_from_directory, render_template_string
from flask_socketio import SocketIO, send, emit
import os
UPLOAD_FOLDER = 'static'
IMAGE_FILENAME = 'latest_image.jpg'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
socketio = SocketIO(app)
# Создание директории, если она не существует
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'photo' not in request.files:
return "No file part", 400
file = request.files['photo']
if file.filename == '':
return "No selected file", 400
save_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(save_path)
return f"File uploaded successfully and saved to {save_path}", 200
@app.route('/image', methods=['GET'])
def get_image():
return send_from_directory(UPLOAD_FOLDER, IMAGE_FILENAME)
@app.route('/')
def index():
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera Image</title>
</head>
<body>
<h1>Latest Image</h1>
<img id="cameraImage" src="/image" alt="Image" style="width:100%;">
<script>
setInterval(function(){
var image = document.getElementById("cameraImage");
image.src = "/image?" + new Date().getTime();
}, 10000); // обновление каждые 10 секунд
</script>
</body>
</html>
'''
return render_template_string(html)
@app.route('/chat')
def chat():
return flask.render_template('chat.html')
@app.route('/chat2')
def chat2():
return flask.render_template('chat2.html')
@socketio.on('message')
def handle_message(message):
print('Received message: ' + message)
send(message, broadcast=True)
@app.route('/upload_form')
def upload_form():
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Image</title>
</head>
<body>
<h1>Upload Image</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="photo" accept="image/*">
<button type="submit">Upload</button>
</form>
<div id="message"></div>
</body>
</html>
'''
return render_template_string(html)
@socketio.on('message')
def handle_message(msg):
print('Message: ' + msg)
send(msg, broadcast=True)
@socketio.on('json')
def handle_json(json):
print('JSON: ' + str(json))
send(json, json=True, broadcast=True)
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)
|