Skip to content

Commit debdbde

Browse files
author
Dan
committed
Cleaned up ssh client imports. Added event loop yield before blocking to accept connections in embedded server
1 parent b8e4ac0 commit debdbde

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

embedded_server/embedded_server.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from .stub_sftp import StubSFTPServer
4242
from .tunnel import Tunneler
4343
import gevent.subprocess
44+
# import gipc
4445

4546
logger = logging.getLogger("embedded_server")
4647
paramiko_logger = logging.getLogger('paramiko.transport')
@@ -168,6 +169,7 @@ def _handle_ssh_connection(transport, fail_auth=False,
168169
except Exception:
169170
logger.exception("Error occured starting server")
170171
return
172+
gevent.sleep(0)
171173
channel = transport.accept(20)
172174
if not channel:
173175
logger.error("Could not establish channel")
@@ -206,6 +208,14 @@ def start_server(sock, fail_auth=False, ssh_exception=False,
206208
return gevent.spawn(listen, sock, fail_auth=fail_auth,
207209
timeout=timeout, ssh_exception=ssh_exception)
208210

211+
# def start_server_process(listen_ip, fail_auth=False, ssh_exception=False,
212+
# timeout=None):
213+
# p = gipc.start_process(target=_make_sock_start_server, args=(listen_ip,),
214+
# kwargs={'fail_auth': fail_auth,
215+
# 'timeout': timeout,
216+
# 'ssh_exception': ssh_exception,})
217+
# return p
218+
209219
if __name__ == "__main__":
210220
logging.basicConfig()
211221
logger.setLevel(logging.DEBUG)

pssh/ssh_client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
"""Package containing SSHClient class."""
2020

2121
import sys
22-
if 'threading' in sys.modules:
23-
del sys.modules['threading']
24-
import gevent
25-
from gevent import monkey
26-
monkey.patch_all()
22+
from gevent import sleep
2723
import logging
2824
import paramiko
2925
from paramiko.ssh_exception import ChannelException as channel_exception
@@ -139,7 +135,7 @@ def _connect_tunnel(self):
139135
proxy_channel = self.proxy_client.get_transport().\
140136
open_channel('direct-tcpip', (self.host, self.port,),
141137
('127.0.0.1', 0))
142-
gevent.sleep(0)
138+
sleep(0)
143139
return self._connect(self.client, self.host, self.port, sock=proxy_channel)
144140
except channel_exception, ex:
145141
error_type = ex.args[1] if len(ex.args) > 1 else ex.args[0]
@@ -164,7 +160,7 @@ def _connect(self, client, host, port, sock=None, retries=1):
164160
logger.error("Could not resolve host '%s' - retry %s/%s",
165161
self.host, retries, self.num_retries)
166162
while retries < self.num_retries:
167-
gevent.sleep(5)
163+
sleep(5)
168164
return self._connect(client, host, port, sock=sock,
169165
retries=retries+1)
170166
raise UnknownHostException("Unknown host %s - %s - retry %s/%s",
@@ -174,7 +170,7 @@ def _connect(self, client, host, port, sock=None, retries=1):
174170
logger.error("Error connecting to host '%s:%s' - retry %s/%s",
175171
self.host, self.port, retries, self.num_retries)
176172
while retries < self.num_retries:
177-
gevent.sleep(5)
173+
sleep(5)
178174
return self._connect(client, host, port, sock=sock,
179175
retries=retries+1)
180176
error_type = ex.args[1] if len(ex.args) > 1 else ex.args[0]
@@ -231,7 +227,7 @@ def exec_command(self, command, sudo=False, user=None, **kwargs):
231227
logger.debug("Running command %s on %s", command, self.host)
232228
channel.exec_command(command, **kwargs)
233229
logger.debug("Command started")
234-
gevent.sleep(0)
230+
sleep(0)
235231
return channel, self.host, stdout, stderr
236232

237233
def _read_output_buffer(self, output_buffer, prefix=''):

0 commit comments

Comments
 (0)