Update build script for client/server use.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import pickle
|
||||
import socket
|
||||
|
||||
@@ -8,7 +9,7 @@ import os
|
||||
from common import util, models
|
||||
from dsst_server import read_functions, write_functions, tokens
|
||||
from dsst_server.func_proxy import FunctionProxy
|
||||
from dsst_server.data_access import sql
|
||||
from dsst_server.data_access import sql, sql_func
|
||||
|
||||
PORT = 12345
|
||||
HOST = socket.gethostname()
|
||||
@@ -16,16 +17,18 @@ BUFFER_SIZE = 1024
|
||||
|
||||
|
||||
class DsstServer:
|
||||
def __init__(self):
|
||||
def __init__(self, config={}):
|
||||
self.socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
print('Created socket')
|
||||
|
||||
self.socket_server.bind((HOST, PORT))
|
||||
print(f'Bound socket to {PORT} on host {HOST}')
|
||||
print('Bound socket to {} on host {}'.format(PORT, HOST))
|
||||
|
||||
# Initialize database
|
||||
sql.db.init('dsst', user='dsst', password='dsst')
|
||||
print(f'Database initialized ({sql.db.database})')
|
||||
db_config = config.get('database')
|
||||
sql.db.init(db_config.get('db_name'), user=db_config.get('user'), password=db_config.get('password'))
|
||||
sql_func.create_tables()
|
||||
print('Database initialized ({})'.format(sql.db.database))
|
||||
|
||||
# Load access tokens and map them to their allowed methods
|
||||
read_actions = util.list_class_methods(read_functions.ReadFunctions)
|
||||
@@ -35,7 +38,7 @@ class DsstServer:
|
||||
'rw': read_actions + write_actions
|
||||
}
|
||||
self.tokens = {token: parm_access[perms] for token, perms in tokens.TOKENS}
|
||||
print(f'Loaded auth tokens: {self.tokens.keys()}')
|
||||
print('Loaded auth tokens: {}'.format(self.tokens.keys()))
|
||||
|
||||
def run(self):
|
||||
self.socket_server.listen(5)
|
||||
@@ -44,15 +47,15 @@ class DsstServer:
|
||||
while True:
|
||||
client, address = self.socket_server.accept()
|
||||
try:
|
||||
print(f'Connection from {address}')
|
||||
print('Connection from {}'.format(address))
|
||||
data = util.recv_msg(client)
|
||||
request = pickle.loads(data)
|
||||
print(f'Request: {request}')
|
||||
print('Request: {}'.format(request))
|
||||
# Validate auth token in request
|
||||
token = request.get('auth_token')
|
||||
if token not in self.tokens:
|
||||
util.send_msg(client, pickle.dumps({'success': False, 'message': 'Auth token invalid'}))
|
||||
print(f'Rejected request from {address}. Auth token invalid ({token})')
|
||||
print('Rejected request from {}. Auth token invalid ({})'.format(address, token))
|
||||
continue
|
||||
# Check read functions
|
||||
action_name = request.get('action')
|
||||
@@ -61,14 +64,14 @@ class DsstServer:
|
||||
try:
|
||||
value = action(request.get('args'))
|
||||
except Exception as e:
|
||||
response = {'success': False, 'message': f'Exception was thrown on server.\n{e}'}
|
||||
response = {'success': False, 'message': 'Exception was thrown on server.\n{}'.format(e)}
|
||||
util.send_msg(client, pickle.dumps(response))
|
||||
raise
|
||||
response = {'success': True, 'data': value}
|
||||
util.send_msg(client, pickle.dumps(response))
|
||||
continue
|
||||
else:
|
||||
msg = f'Action does not exist on server ({request.get("action")})'
|
||||
msg = 'Action does not exist on server ({})'.format(request.get('action'))
|
||||
util.send_msg(client, pickle.dumps({'success': False, 'message': msg}))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -77,8 +80,14 @@ class DsstServer:
|
||||
print('Connection to client closed')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
server = DsstServer()
|
||||
def load_config(config_path: str) -> dict:
|
||||
with open(config_path) as config_file:
|
||||
return json.load(config_file)
|
||||
|
||||
|
||||
def main():
|
||||
config = os.path.join(os.path.expanduser('~'), '.config', 'dsst', 'server.json')
|
||||
server = DsstServer(load_config(config))
|
||||
try:
|
||||
server.run()
|
||||
except KeyboardInterrupt:
|
||||
|
||||
Reference in New Issue
Block a user