Skip to content

Commit ee83fd6

Browse files
author
Dan
committed
Added unicode output test and unicode encoding in embedded server commands
1 parent 19c39e1 commit ee83fd6

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

embedded_server/embedded_server.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,22 @@ def check_channel_forward_agent_request(self, channel):
100100
logger.debug("Forward agent key request for channel %s" % (channel,))
101101
return True
102102

103-
def check_channel_exec_request(self, channel, cmd):
103+
def check_channel_exec_request(self, channel, cmd,
104+
encoding='utf-8'):
104105
logger.debug("Got exec request on channel %s for cmd %s" % (channel, cmd,))
105106
self.event.set()
106-
process = gevent.subprocess.Popen(cmd, stdout=gevent.subprocess.PIPE, shell=True)
107+
_env = os.environ
108+
_env['PYTHONIOENCODING'] = encoding
109+
process = gevent.subprocess.Popen(cmd, stdout=gevent.subprocess.PIPE,
110+
stdin=gevent.subprocess.PIPE,
111+
shell=True, env=_env)
107112
gevent.spawn(self._read_response, channel, process)
108113
return True
109114

110115
def _read_response(self, channel, process):
111-
for line in process.stdout:
112-
channel.send(line.decode('ascii'))
113-
process.communicate()
116+
(output, _) = process.communicate()
117+
for line in output:
118+
channel.send(line)
114119
channel.send_exit_status(process.returncode)
115120
logger.debug("Command finished with return code %s", process.returncode)
116121
# Let clients consume output from channel before closing
@@ -138,11 +143,13 @@ def listen(sock, fail_auth=False, ssh_exception=False,
138143
"""
139144
listen_ip, listen_port = sock.getsockname()
140145
if not sock:
141-
logger.error("Could not establish listening connection on %s:%s", listen_ip, listen_port)
146+
logger.error("Could not establish listening connection on %s:%s",
147+
listen_ip, listen_port)
142148
return
143149
try:
144150
sock.listen(100)
145-
logger.info('Listening for connection on %s:%s..', listen_ip, listen_port)
151+
logger.info('Listening for connection on %s:%s..', listen_ip,
152+
listen_port)
146153
except Exception, e:
147154
logger.error('*** Listen failed: %s' % (str(e),))
148155
traceback.print_exc()
@@ -179,7 +186,7 @@ def _handle_ssh_connection(transport, fail_auth=False,
179186
while not channel.send_ready():
180187
gevent.sleep(.2)
181188
channel.close()
182-
189+
183190
def handle_ssh_connection(sock,
184191
fail_auth=False, ssh_exception=False,
185192
timeout=None):

tests/test_ssh_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23

34
# This file is part of parallel-ssh.
45

@@ -247,6 +248,19 @@ def test_ssh_client_pty(self):
247248
del channel
248249
del client
249250

251+
def test_ssh_client_utf_encoding(self):
252+
"""Test that unicode output works"""
253+
client = SSHClient(self.host, port=self.listen_port,
254+
pkey=self.user_key)
255+
expected = [u'é']
256+
cmd = u"echo 'é'"
257+
channel, host, stdout, stderr = client.exec_command(cmd)
258+
output = list(stdout)
259+
self.assertEqual(expected, output,
260+
msg="Got unexpected unicode output %s - expected %s" % (
261+
output, expected,))
262+
del client
263+
250264
def test_ssh_client_pty(self):
251265
"""Test that running command sans shell works as expected
252266
and that shell commands fail accordingly"""

0 commit comments

Comments
 (0)