2019-10-12 19:48:19 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Star Inc. multiprocessing data switching
|
|
|
|
===
|
2019-10-14 20:43:06 +08:00
|
|
|
To switch data in multiprocessing.
|
2019-10-12 19:48:19 +08:00
|
|
|
|
|
|
|
LICENSE: MPL 2.0
|
2020-02-07 20:35:26 +08:00
|
|
|
(c)2020 Star Inc.
|
2019-10-12 19:48:19 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Initializing
|
|
|
|
import json
|
|
|
|
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado.web import Application, RequestHandler
|
|
|
|
|
|
|
|
switch_data = {}
|
|
|
|
auth_code = 0
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2019-10-12 19:48:19 +08:00
|
|
|
# Functions
|
2020-02-07 20:35:26 +08:00
|
|
|
def update(data):
|
2019-10-12 19:48:19 +08:00
|
|
|
global switch_data
|
2020-02-12 15:01:03 +08:00
|
|
|
# noinspection PyBroadException
|
2019-10-12 19:48:19 +08:00
|
|
|
try:
|
2020-02-07 20:35:26 +08:00
|
|
|
if type(data["path"]) is list:
|
|
|
|
over = query({"path": data["path"]})
|
|
|
|
over.get("data").update(data["data"])
|
2019-12-25 21:35:43 +08:00
|
|
|
return {"status": 200}
|
|
|
|
return {"status": 400}
|
2019-10-12 19:48:19 +08:00
|
|
|
except:
|
|
|
|
return {"status": 500}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-07 20:35:26 +08:00
|
|
|
def delete(data):
|
2019-10-12 19:48:19 +08:00
|
|
|
global switch_data
|
2020-02-12 15:01:03 +08:00
|
|
|
# noinspection PyBroadException
|
2019-10-12 19:48:19 +08:00
|
|
|
try:
|
2020-02-07 20:35:26 +08:00
|
|
|
if type(data["path"]) is list:
|
|
|
|
over = query({"path": data["path"]})
|
|
|
|
over.get("data").pop(data["data"])
|
2019-12-25 21:35:43 +08:00
|
|
|
return {"status": 200}
|
|
|
|
return {"status": 400}
|
2019-10-12 19:48:19 +08:00
|
|
|
except:
|
|
|
|
return {"status": 500}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-07 20:35:26 +08:00
|
|
|
def query(data):
|
2019-10-12 19:48:19 +08:00
|
|
|
global switch_data
|
2020-02-07 20:35:26 +08:00
|
|
|
query_data = data["path"]
|
2020-02-12 15:01:03 +08:00
|
|
|
# noinspection PyBroadException
|
2019-10-12 19:48:19 +08:00
|
|
|
try:
|
|
|
|
if type(switch_data) is dict and type(query_data) is list:
|
2019-10-14 20:40:20 +08:00
|
|
|
result = switch_data
|
2019-10-27 22:44:57 +08:00
|
|
|
query_len = len(query_data) - 1
|
2019-10-12 19:48:19 +08:00
|
|
|
for count, key in enumerate(query_data):
|
2019-10-27 22:44:57 +08:00
|
|
|
if key in result:
|
|
|
|
if count < query_len:
|
|
|
|
if type(result.get(key)) is not dict:
|
2019-12-25 21:35:43 +08:00
|
|
|
result = 1 # "unknown_type" + type(source_data.get(key))
|
2019-10-12 19:48:19 +08:00
|
|
|
break
|
2019-10-27 22:44:57 +08:00
|
|
|
result = result.get(key)
|
2019-10-12 19:48:19 +08:00
|
|
|
else:
|
2019-12-25 21:35:43 +08:00
|
|
|
result = 2 # "unknown_key"
|
2019-10-12 19:48:19 +08:00
|
|
|
break
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
return {"status": 200, "data": result}
|
|
|
|
return {"status": 400}
|
2019-10-12 19:48:19 +08:00
|
|
|
except:
|
|
|
|
return {"status": 500}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-07 20:35:26 +08:00
|
|
|
def sync(data):
|
2019-10-12 19:48:19 +08:00
|
|
|
global switch_data
|
2020-02-12 15:01:03 +08:00
|
|
|
# noinspection PyBroadException
|
2019-10-12 19:48:19 +08:00
|
|
|
try:
|
2020-02-07 20:35:26 +08:00
|
|
|
switch_data = data["path"]
|
2019-12-25 21:35:43 +08:00
|
|
|
return {"status": 200}
|
2019-10-12 19:48:19 +08:00
|
|
|
except:
|
|
|
|
return {"status": 500}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2020-02-07 20:35:26 +08:00
|
|
|
def yuukiLimitDecrease(data):
|
2019-10-12 21:29:38 +08:00
|
|
|
global switch_data
|
2020-02-12 15:01:03 +08:00
|
|
|
# noinspection PyBroadException
|
2019-10-12 21:29:38 +08:00
|
|
|
try:
|
2020-02-07 20:35:26 +08:00
|
|
|
switch_data["LimitInfo"][data["path"]][data["userId"]] -= 1
|
2019-12-25 21:35:43 +08:00
|
|
|
return {"status": 200}
|
2019-10-12 21:29:38 +08:00
|
|
|
except:
|
|
|
|
return {"status": 500}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2019-10-12 19:48:19 +08:00
|
|
|
# Works
|
|
|
|
_work = {
|
|
|
|
"UPT": update,
|
|
|
|
"DEL": delete,
|
|
|
|
"GET": query,
|
2019-10-12 21:29:38 +08:00
|
|
|
"SYC": sync,
|
|
|
|
"YLD": yuukiLimitDecrease
|
2019-10-12 19:48:19 +08:00
|
|
|
}
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2019-10-12 19:48:19 +08:00
|
|
|
class IndexHandler(RequestHandler):
|
|
|
|
def get(self):
|
|
|
|
self.write('''
|
|
|
|
<b>Python MDS Server</b><br>
|
2019-10-14 20:43:06 +08:00
|
|
|
To switch data in multiprocessing.<hr>
|
2019-10-12 19:48:19 +08:00
|
|
|
(c)2019 <a href="https://starinc.xyz">Star Inc.</a>
|
|
|
|
''')
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
global auth_code
|
|
|
|
req_body = self.request.body
|
|
|
|
req_str = req_body.decode('utf8')
|
|
|
|
req_res = json.loads(req_str)
|
|
|
|
if req_res.get("code") == auth_code:
|
2020-02-07 20:35:26 +08:00
|
|
|
result = _work[req_res.get("do")](
|
|
|
|
{
|
2020-02-25 21:02:14 +08:00
|
|
|
"path": req_res.get("path"),
|
|
|
|
"data": req_res.get("data")
|
2020-02-07 20:35:26 +08:00
|
|
|
}
|
|
|
|
)
|
2019-10-12 19:48:19 +08:00
|
|
|
else:
|
2019-12-25 21:35:43 +08:00
|
|
|
result = {"status": 401}
|
2019-10-12 19:48:19 +08:00
|
|
|
if not result:
|
2019-12-25 21:35:43 +08:00
|
|
|
result = {"status": 500}
|
2019-10-12 19:48:19 +08:00
|
|
|
self.write(json.dumps(result))
|
|
|
|
|
2019-12-25 21:35:43 +08:00
|
|
|
|
2019-10-12 19:48:19 +08:00
|
|
|
# Main
|
2020-02-26 21:50:49 +08:00
|
|
|
app = Application([
|
|
|
|
('/', IndexHandler)
|
|
|
|
])
|
|
|
|
server = HTTPServer(app)
|
|
|
|
async_lock = IOLoop.current()
|
|
|
|
|
|
|
|
|
2019-10-12 19:48:19 +08:00
|
|
|
def listen(code):
|
|
|
|
global auth_code
|
|
|
|
auth_code = code
|
|
|
|
server.listen(2019)
|
2020-02-26 21:50:49 +08:00
|
|
|
async_lock.start()
|
|
|
|
|
|
|
|
|
|
|
|
def mds_exit():
|
|
|
|
server.stop()
|
|
|
|
yield server.close_all_connections()
|
|
|
|
async_lock.stop()
|
|
|
|
async_lock.close()
|