Add socket server stub

This commit is contained in:
nolash 2020-08-05 01:04:20 +02:00
parent fafef39943
commit a365162086
Signed by: lash
GPG Key ID: 93EC1C676274C889

44
scripts/server.py Normal file
View File

@ -0,0 +1,44 @@
import socket
import json
import logging
from jsonrpc.exceptions import JSONRPCParseError
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
def jsonrpc_error(id, err):
return {
'json-rpc': '2.0',
'error': {
'code': err.CODE,
'message': err.MESSAGE,
},
}
def jsonrpc_ok(rpc_id, response):
return {
'json-rpc': '2.0',
'id': rpc_id,
'response': response,
}
s = socket.socket(family = socket.AF_UNIX, type = socket.SOCK_STREAM)
s.bind('/tmp/foo.ipc')
s.listen(10)
while True:
(csock, caddr) = s.accept()
d = csock.recv(4096)
try:
logg.debug('{}'.format(d.decode('utf-8')))
json.loads(d)
csock.send(json.dumps(jsonrpc_ok(0, [])).encode('utf-8'))
except:
csock.send(json.dumps(jsonrpc_error(None, JSONRPCParseError)).encode('utf-8'))
csock.close()
s.close()
os.unlink('/tmp/foo.ipc')