@@ -103,7 +103,8 @@ class Server(paramiko.ServerInterface):
103103 """
104104
105105 def __init__ (self , transport , host_key , fail_auth = False ,
106- ssh_exception = False ):
106+ ssh_exception = False ,
107+ encoding = 'utf-8' ):
107108 paramiko .ServerInterface .__init__ (self )
108109 transport .load_server_moduli ()
109110 transport .add_server_key (host_key )
@@ -113,6 +114,7 @@ def __init__(self, transport, host_key, fail_auth=False,
113114 self .fail_auth = fail_auth
114115 self .ssh_exception = ssh_exception
115116 self .host_key = host_key
117+ self .encoding = encoding
116118
117119 def check_channel_request (self , kind , chanid ):
118120 return paramiko .OPEN_SUCCEEDED
@@ -163,12 +165,11 @@ def check_channel_forward_agent_request(self, channel):
163165 gevent .sleep ()
164166 return True
165167
166- def check_channel_exec_request (self , channel , cmd ,
167- encoding = 'utf-8' ):
168+ def check_channel_exec_request (self , channel , cmd ):
168169 logger .debug ("Got exec request on channel %s for cmd %s" % (channel , cmd ,))
169170 self .event .set ()
170171 _env = os .environ
171- _env ['PYTHONIOENCODING' ] = encoding
172+ _env ['PYTHONIOENCODING' ] = self . encoding
172173 if hasattr (channel , 'environment' ):
173174 _env .update (channel .environment )
174175 process = gevent .subprocess .Popen (cmd , stdout = gevent .subprocess .PIPE ,
@@ -182,7 +183,8 @@ def check_channel_exec_request(self, channel, cmd,
182183 def check_channel_env_request (self , channel , name , value ):
183184 if not hasattr (channel , 'environment' ):
184185 channel .environment = {}
185- channel .environment .update ({name .decode ('utf8' ): value .decode ('utf8' )})
186+ channel .environment .update ({
187+ name .decode (self .encoding ): value .decode (self .encoding )})
186188 return True
187189
188190 def _read_response (self , channel , process ):
@@ -213,7 +215,8 @@ def make_socket(listen_ip, port=0):
213215 return sock
214216
215217def listen (sock , fail_auth = False , ssh_exception = False ,
216- timeout = None ):
218+ timeout = None ,
219+ encoding = 'utf-8' ):
217220 """Run server and given a cmd_to_run, send given
218221 response to client connection. Returns (server, socket) tuple
219222 where server is a joinable server thread and socket is listening
@@ -228,13 +231,16 @@ def listen(sock, fail_auth=False, ssh_exception=False,
228231 return
229232 host , port = sock .getsockname ()
230233 logger .info ('Listening for connection on %s:%s..' , host , port )
231- return handle_ssh_connection (sock , fail_auth = fail_auth ,
232- timeout = timeout , ssh_exception = ssh_exception )
234+ return handle_ssh_connection (
235+ sock , fail_auth = fail_auth , timeout = timeout , ssh_exception = ssh_exception ,
236+ encoding = encoding )
233237
234238def _handle_ssh_connection (transport , fail_auth = False ,
235- ssh_exception = False ):
239+ ssh_exception = False ,
240+ encoding = 'utf-8' ):
236241 server = Server (transport , host_key ,
237- fail_auth = fail_auth , ssh_exception = ssh_exception )
242+ fail_auth = fail_auth , ssh_exception = ssh_exception ,
243+ encoding = encoding )
238244 try :
239245 transport .start_server (server = server )
240246 except paramiko .SSHException as e :
@@ -261,7 +267,8 @@ def _handle_ssh_connection(transport, fail_auth=False,
261267
262268def handle_ssh_connection (sock ,
263269 fail_auth = False , ssh_exception = False ,
264- timeout = None ):
270+ timeout = None ,
271+ encoding = 'utf-8' ):
265272 conn , addr = sock .accept ()
266273 logger .info ('Got connection..' )
267274 if timeout :
@@ -271,7 +278,8 @@ def handle_ssh_connection(sock,
271278 try :
272279 transport = paramiko .Transport (conn )
273280 return _handle_ssh_connection (transport , fail_auth = fail_auth ,
274- ssh_exception = ssh_exception )
281+ ssh_exception = ssh_exception ,
282+ encoding = encoding )
275283 except Exception as e :
276284 logger .error ('*** Caught exception: %s: %s' % (str (e .__class__ ), str (e ),))
277285 traceback .print_exc ()
@@ -281,14 +289,18 @@ def handle_ssh_connection(sock,
281289 pass
282290
283291def start_server (sock , fail_auth = False , ssh_exception = False ,
284- timeout = None ):
292+ timeout = None ,
293+ encoding = 'utf-8' ):
285294 return gevent .spawn (listen , sock , fail_auth = fail_auth ,
286- timeout = timeout , ssh_exception = ssh_exception )
295+ timeout = timeout , ssh_exception = ssh_exception ,
296+ encoding = encoding )
287297
288298def start_server_from_ip (ip , port = 0 ,
289299 fail_auth = False , ssh_exception = False ,
290- timeout = None ):
300+ timeout = None ,
301+ encoding = 'utf-8' ):
291302 server_sock = make_socket (ip , port = port )
292303 server = start_server (server_sock , fail_auth = fail_auth ,
293- ssh_exception = ssh_exception , timeout = timeout )
304+ ssh_exception = ssh_exception , timeout = timeout ,
305+ encoding = encoding )
294306 return server , server_sock .getsockname ()[1 ]
0 commit comments