This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
ipc-unix/ipc_unix/server.py
2018-12-07 20:44:31 +00:00

41 lines
1.1 KiB
Python

import socketserver
import threading
import ujson
from ipc_unix.utils import read_payload
class RequestHandler(socketserver.BaseRequestHandler):
def handle_request(self, request):
raise NotImplementedError("Failed to override `handle_request`")
def handle(self):
data = read_payload(self.request)
response = self.handle_request(data)
self.request.sendall(ujson.dumps(response).encode())
class Server:
def __init__(self, socket_path):
class InstanceRequestHandler(RequestHandler):
handle_request = self.handle_request
self.server = socketserver.UnixStreamServer(socket_path, InstanceRequestHandler)
def serve_forever(self):
self.server.serve_forever()
def serve_in_thread(self):
thread = threading.Thread(target=self.serve_forever)
thread.start()
return thread
def shutdown(self):
self.server.shutdown()
def close(self):
self.shutdown()
self.server.server_close()
def handle_request(self, request):
raise NotImplementedError("Must override `handle_request`")