Skip to content

Commit

Permalink
Merge pull request #4 from tryexceptpass/fixes
Browse files Browse the repository at this point in the history
* Added try / except around server response processing - closes #1
* Added max_packet_size option to KorvClient - closes #2.
* Updated requirements.txt dependencies.
* Fixed logging bug - closes #3.
* Fixed missing `callback` parameter in STORE, UPDATE, DELETE.
* Bumped version to 0.1.3 and did some light linting.
  • Loading branch information
tryexceptpass authored Jan 8, 2019
2 parents 4c74f89 + ad37bc4 commit 2fb0340
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
32 changes: 19 additions & 13 deletions korv/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ def session_started(self):
def data_received(self, data, datatype):
logging.debug(f"Received data: {data}")

data = json.loads(data)
if data['request_id'] in self._requests:
if callable(self._requests[data['request_id']]):
self._requests[data['request_id']](data)
try:
data = json.loads(data)
if data['request_id'] in self._requests:
if callable(self._requests[data['request_id']]):
self._requests[data['request_id']](data)

if self._requests[data['request_id']] is None:
self._requests[data['request_id']] = data
else:
del(self._requests[data['request_id']])

if self._requests[data['request_id']] is None:
self._requests[data['request_id']] = data
else:
del(self._requests[data['request_id']])
except Exception:
logging.exception(f"There was an error processing the server response")

def eof_received(self):
logging.debug("Received EOF")
Expand All @@ -68,7 +72,9 @@ async def send_request(self, verb, resource, body, callback):

class KorvClient:

def __init__(self, host='localhost', port=8022, client_keys=None, known_hosts=None):
def __init__(self, host='localhost', port=8022, client_keys=None, known_hosts=None, max_packet_size=None):
self.max_packet_size = max_packet_size

self._loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._loop)
self._session = asyncio.get_event_loop().run_until_complete(self.__connect(host, port, known_hosts, client_keys))
Expand Down Expand Up @@ -96,7 +102,7 @@ async def __connect(self, host, port, known_hosts, client_keys):
)

logging.debug("Opening Socket")
chan, session = await conn.create_connection(_SSHClientSession, host, port)
chan, session = await conn.create_connection(_SSHClientSession, host, port, max_pktsize=self.max_packet_size)
return session

def get(self, resource, body=None, callback=None):
Expand All @@ -113,11 +119,11 @@ def get(self, resource, body=None, callback=None):
else:
asyncio.run_coroutine_threadsafe(self._session.send_request("GET", resource, body, callback), self._loop)

def store(self, resource, body):
def store(self, resource, body, callback=None):
asyncio.run_coroutine_threadsafe(self._session.send_request("STORE", resource, body, callback), self._loop)

def update(self, resource, body):
def update(self, resource, body, callback=None):
asyncio.run_coroutine_threadsafe(self._session.send_request("UPDATE", resource, body, callback), self._loop)

def delete(self, resource, body=None):
def delete(self, resource, body=None, callback=None):
asyncio.run_coroutine_threadsafe(self._session.send_request("DELETE", resource, body, callback), self._loop)
5 changes: 3 additions & 2 deletions korv/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _send_response(self, request_id, code, body=None):
class KorvServer(asyncssh.SSHServer):
VERBS = ('GET', 'STORE', 'UPDATE', 'DELETE')

_callbacks = { verb: dict() for verb in VERBS}
_callbacks = {verb: dict() for verb in VERBS}

def __init__(self, port=8022, host_keys=['ssh_host_key'], authorized_client_keys='authorized_keys'):
"""Instatiate an SSH server that listens on the given port for clients that match the authorized keys"""
Expand All @@ -118,7 +118,7 @@ def __init__(self, port=8022, host_keys=['ssh_host_key'], authorized_client_keys
def connection_requested(self, dest_host, dest_port, orig_host, orig_port):
"""Run a new TCP session that handles an SSH client connection"""

logging.info("Connection requested", dest_host, dest_port, orig_host, orig_port)
logging.info(f"Connection requested {dest_host} {dest_port} {orig_host} {orig_port}")
return _KorvServerSession(KorvServer._callbacks)

async def __create_server(self):
Expand Down Expand Up @@ -150,6 +150,7 @@ def start(self):

try:
loop.run_until_complete(self.__create_server())

except (OSError, asyncssh.Error) as exc:
sys.exit(f'Error starting server: {exc}')

Expand Down
8 changes: 1 addition & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
asn1crypto==0.24.0
asyncssh==1.12.1
cffi==1.11.5
cryptography>=2.3
idna==2.6
pycparser==2.18
six==1.11.0
asyncssh==1.15.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
author_email = "[email protected]",

name = "korv",
version = "0.1.1",
version = "0.1.3",
description = "SSH API Frameowrk",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 2fb0340

Please sign in to comment.