Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions mockssh/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import socket
import subprocess
import threading

import time
import fcntl
try:
from queue import Queue
except ImportError: # Python 2.7
Expand Down Expand Up @@ -55,9 +56,26 @@ def handle_client(self, channel):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
channel.sendall(stdout)
channel.sendall_stderr(stderr)

# Configure descriptors as non blocking
fcntl.fcntl(p.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(p.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

while p.poll() is None:
r_ready, w_ready, x_ready = select.select(
[channel, p.stdout, p.stderr], [p.stdin], []
)

if channel.recv_ready() and p.stdin in w_ready:
p.stdin.write(recv_all(channel))
p.stdin.flush()

if p.stdout in r_ready:
channel.sendall(read_nonblock(p.stdout))

if p.stderr in r_ready:
channel.sendall_stderr(read_nonblock(p.stderr))

channel.send_exit_status(p.returncode)
except Exception:
self.log.error("Error handling client (channel: %s)", channel,
Expand Down Expand Up @@ -161,3 +179,15 @@ def port(self):
@property
def users(self):
return self._users.keys()


def read_nonblock(f):
while True:
try:
return f.read()
except IOError:
time.sleep(.1)


def recv_all(channel):
return b''.join(iter(lambda: channel.recv(1024), ''))