-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.py
37 lines (32 loc) · 1.01 KB
/
wss.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
try: input=raw_input
except: pass
try: from websocket_server import WebsocketServer
except:
print('I need to install websocket-server. Enter y if this is OK.')
if input()!='y': import sys; sys.exit(1)
import subprocess
subprocess.check_call('sudo pip install websocket-server', shell=True)
from websocket_server import WebsocketServer
import json
def handle_message(client, server, message):
try: j=json.loads(message)
except Exception as e:
print(e)
return
j['result']={}
j['result']['success']=True
try:
if j['command']=='load':
with open(j['path'], 'r') as file: j['result']['contents']=file.read()
if j['command']=='save':
import os
try: os.makedirs(os.path.split(j['path'])[0])
except: pass
with open(j['path'], 'w') as file: file.write(j['contents'])
except Exception as e:
j['result']['success']=False
j['result']['error']=str(e)
server.send_message(client, json.dumps(j))
server=WebsocketServer(9160, host='localhost')
server.set_fn_message_received(handle_message)
server.run_forever()