-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocketserver.py
More file actions
37 lines (32 loc) · 1.35 KB
/
Copy pathwebsocketserver.py
File metadata and controls
37 lines (32 loc) · 1.35 KB
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
# pyinstaller websocketserver.py --onefile --icon="C:\Users\emo\python\images\proxy.ico"
import websockets
import asyncio
import time
import sqlite3
import json
async def websocket_request_handler(websocket, path):
client_data = await websocket.recv()
print(time.ctime() + '\n' + client_data + '\n\r')
response_data = "Datos recibidos por el cliente:\n" + client_data
await websocket.send(response_data)
save_to_database(client_data)
def save_to_database(client_data):
conn = sqlite3.connect('datos.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS datos
(id INTEGER PRIMARY KEY AUTOINCREMENT,
identificador TEXT,
time TEXT,
text TEXT)''')
data = json.loads(client_data)
c.execute('''INSERT INTO datos (id, time, text) VALUES (?, ?, ?)''', (data['identificador'], data['time'], data['text']))
conn.commit()
conn.close()
async def start_websocket_server(host, port_number):
async with websockets.serve(websocket_request_handler, host, port_number):
print('websocket server en el puerto : ' + str(port_number))
await asyncio.Future() # espera infinita
if __name__ == '__main__':
host = "localhost"
port_number = 9999
asyncio.run(start_websocket_server(host, port_number))