DmitrMakeev commited on
Commit
4454f44
·
verified ·
1 Parent(s): 598cf95

Update app.py

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