-
-
Save pemn/06238ea510b36504698237b5ae035ebe to your computer and use it in GitHub Desktop.
Simple socket IPC in python (updated for python3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socketserver | |
import socket | |
import struct | |
import json | |
import collections | |
class IPCError(Exception): | |
pass | |
class UnknownMessageClass(IPCError): | |
pass | |
class InvalidSerialization(IPCError): | |
pass | |
class ConnectionClosed(IPCError): | |
pass | |
def _read_objects(sock): | |
header = sock.recv(4) | |
if len(header) == 0: | |
raise ConnectionClosed() | |
size = struct.unpack('!i', header)[0] | |
data = sock.recv(size - 4) | |
if len(data) == 0: | |
raise ConnectionClosed() | |
return Message.deserialize(json.loads(data.decode())) | |
def _write_objects(sock, objects): | |
data = json.dumps([o.serialize() for o in objects]) | |
sock.sendall(struct.pack('!i', len(data) + 4)) | |
sock.sendall(bytes(data, 'UTF-8')) | |
def _recursive_subclasses(cls): | |
classmap = {} | |
for subcls in cls.__subclasses__(): | |
classmap[subcls.__name__] = subcls | |
classmap.update(_recursive_subclasses(subcls)) | |
return classmap | |
class Message(object): | |
@classmethod | |
def deserialize(cls, objects): | |
classmap = _recursive_subclasses(cls) | |
serialized = [] | |
for obj in objects: | |
if isinstance(obj, Message): | |
serialized.append(obj) | |
else: | |
try: | |
serialized.append(classmap[obj['class']](*obj['args'], **obj['kwargs'])) | |
except KeyError as e: | |
raise UnknownMessageClass(e) | |
except TypeError as e: | |
raise InvalidSerialization(e) | |
return serialized | |
def serialize(self): | |
args, kwargs = self._get_args() | |
return {'class': type(self).__name__, 'args': args, 'kwargs': kwargs} | |
def _get_args(self): | |
return [], {} | |
def __repr__(self): | |
r = self.serialize() | |
args = ', '.join([repr(arg) for arg in r['args']]) | |
kwargs = ''.join([', {}={}'.format(k, repr(v)) for k, v in list(r['kwargs'].items())]) | |
name = r['class'] | |
return '{}({}{})'.format(name, args, kwargs) | |
class Client(object): | |
def __init__(self, server_address): | |
self.addr = server_address | |
if isinstance(self.addr, str): | |
address_family = socket.AF_UNIX | |
else: | |
address_family = socket.AF_INET | |
self.sock = socket.socket(address_family, socket.SOCK_STREAM) | |
def connect(self): | |
self.sock.connect(self.addr) | |
def close(self): | |
self.sock.close() | |
def __enter__(self): | |
self.connect() | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
self.close() | |
def send(self, objects): | |
_write_objects(self.sock, objects) | |
return _read_objects(self.sock) | |
#class Server(socketserver.ThreadingUnixStreamServer): | |
class Server(socketserver.ThreadingMixIn, socketserver.TCPServer): | |
def __init__(self, server_address, callback, bind_and_activate=True): | |
if not isinstance(callback, collections.Callable): | |
callback = lambda x: [] | |
class IPCHandler(socketserver.BaseRequestHandler): | |
def handle(self): | |
while True: | |
try: | |
results = _read_objects(self.request) | |
except ConnectionClosed as e: | |
return | |
_write_objects(self.request, callback(results)) | |
if isinstance(server_address, str): | |
self.address_family = socket.AF_UNIX | |
else: | |
self.address_family = socket.AF_INET | |
socketserver.TCPServer.__init__(self, server_address, IPCHandler, bind_and_activate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" ipc_example.py | |
Usage: | |
ipc_example.py server [options] | |
ipc_example.py client [options] <class> [--arg=<arg>...] [--kwarg=<kwarg>...] | |
Options: | |
-p --port=<port> The port number to communicate over [default: 5795] | |
-h --host=<host> The host name to communicate over [default: localhost] | |
-s --socket=<socket> A socket file to use, instead of a host:port | |
-a --arg=<arg> A positional arg to supply the class constructor | |
-k --kwarg=<kwarg> A keyword arg to supply the class constructor | |
""" | |
import docopt | |
import ipc | |
class Event(ipc.Message): | |
def __init__(self, event_type, **properties): | |
self.type = event_type | |
self.properties = properties | |
def _get_args(self): | |
return [self.type], self.properties | |
class Response(ipc.Message): | |
def __init__(self, text): | |
self.text = text | |
def _get_args(self): | |
return [self.text], {} | |
def server_process_request(objects): | |
response = [Response('Received %d objects' % len(objects))] | |
print('Received objects: %s' % objects) | |
print('Sent objects: %s' % response) | |
return(response) | |
if __name__ == '__main__': | |
args = docopt.docopt(__doc__) | |
server_address = args['--socket'] or (args['--host'], int(args['--port'])) | |
if args['server']: | |
ipc.Server(server_address, server_process_request).serve_forever() | |
if args['client']: | |
kwargs = {k: v for k, v in [i.split('=', 1) for i in args['--kwarg']]} | |
user_input = [{'class': args['<class>'], 'args': args['--arg'], 'kwargs': kwargs}] | |
objects = ipc.Message.deserialize(user_input) | |
print('Sending objects: {}'.format(objects)) | |
with ipc.Client(server_address) as client: | |
response = client.send(objects) | |
print('Recieved objects: {}'.format(response)) | |
# Example usage: | |
# $ ./ipc_example.py server | |
# then in another terminal: | |
# $ ./ipc_example.py client Event --arg=testevent --kwarg=exampleproperty=examplevalue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment