@@ -73,7 +73,7 @@ def __del__(self):
7373 # Try to disconnect cleanly when class is deleted.
7474 if self ._srv_handler :
7575 try :
76- self ._srv_handler .write ("LOGOUT\n " )
76+ self ._srv_handler .write (b "LOGOUT\n " )
7777 self ._srv_handler .close ()
7878 except (telnetlib .socket .error , AttributeError ):
7979 # The socket is already disconnected.
@@ -98,14 +98,14 @@ def _connect(self):
9898 timeout = self ._timeout )
9999
100100 if self ._login is not None :
101- self ._srv_handler .write ("USERNAME %s\n " % self ._login )
102- result = self ._srv_handler .read_until ("\n " , self ._timeout )
101+ self ._srv_handler .write (b "USERNAME %s\n " % self ._login . encode ( 'utf-8' ) )
102+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
103103 if not result == "OK\n " :
104104 raise PyNUTError (result .replace ("\n " , "" ))
105105
106106 if self ._password is not None :
107- self ._srv_handler .write ("PASSWORD %s\n " % self ._password )
108- result = self ._srv_handler .read_until ("\n " , self ._timeout )
107+ self ._srv_handler .write (b "PASSWORD %s\n " % self ._password . encode ( 'utf-8' ) )
108+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
109109 if not result == "OK\n " :
110110 raise PyNUTError (result .replace ("\n " , "" ))
111111 except telnetlib .socket .error :
@@ -115,8 +115,8 @@ def description(self, ups):
115115 """Returns the description for a given UPS."""
116116 logging .debug ("description called..." )
117117
118- self ._srv_handler .write ("GET UPSDESC %s\n " % ups )
119- result = self ._srv_handler .read_until ("\n " , self ._timeout )
118+ self ._srv_handler .write (b "GET UPSDESC %s\n " % ups . encode ( 'utf-8' ) )
119+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
120120 try :
121121 return result .split ('"' )[1 ].strip ()
122122 except IndexError :
@@ -130,13 +130,13 @@ def list_ups(self):
130130 """
131131 logging .debug ("list_ups from server" )
132132
133- self ._srv_handler .write ("LIST UPS\n " )
134- result = self ._srv_handler .read_until ("\n " , self ._timeout )
133+ self ._srv_handler .write (b "LIST UPS\n " )
134+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
135135 if result != "BEGIN LIST UPS\n " :
136136 raise PyNUTError (result .replace ("\n " , "" ))
137137
138- result = self ._srv_handler .read_until ("END LIST UPS\n " ,
139- self ._timeout )
138+ result = self ._srv_handler .read_until (b "END LIST UPS\n " ,
139+ self ._timeout ). decode ( 'utf-8' )
140140
141141 ups_dict = {}
142142 for line in result .split ("\n " ):
@@ -154,13 +154,13 @@ def list_vars(self, ups):
154154 """
155155 logging .debug ("list_vars called..." )
156156
157- self ._srv_handler .write ("LIST VAR %s\n " % ups )
158- result = self ._srv_handler .read_until ("\n " , self ._timeout )
157+ self ._srv_handler .write (b "LIST VAR %s\n " % ups . encode ( 'utf-8' ) )
158+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
159159 if result != "BEGIN LIST VAR %s\n " % ups :
160160 raise PyNUTError (result .replace ("\n " , "" ))
161161
162- result = self ._srv_handler .read_until ("END LIST VAR %s\n " % ups ,
163- self ._timeout )
162+ result = self ._srv_handler .read_until (b "END LIST VAR %s\n " % ups . encode ( 'utf-8' ) ,
163+ self ._timeout ). decode ( 'utf-8' )
164164 offset = len ("VAR %s " % ups )
165165 end_offset = 0 - (len ("END LIST VAR %s\n " % ups ) + 1 )
166166
@@ -179,13 +179,13 @@ def list_commands(self, ups):
179179 """
180180 logging .debug ("list_commands called..." )
181181
182- self ._srv_handler .write ("LIST CMD %s\n " % ups )
183- result = self ._srv_handler .read_until ("\n " , self ._timeout )
182+ self ._srv_handler .write (b "LIST CMD %s\n " % ups . encode ( 'utf-8' ) )
183+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
184184 if result != "BEGIN LIST CMD %s\n " % ups :
185185 raise PyNUTError (result .replace ("\n " , "" ))
186186
187- result = self ._srv_handler .read_until ("END LIST CMD %s\n " % ups ,
188- self ._timeout )
187+ result = self ._srv_handler .read_until (b "END LIST CMD %s\n " % ups . encode ( 'utf-8' ) ,
188+ self ._timeout ). decode ( 'utf-8' )
189189 offset = len ("CMD %s " % ups )
190190 end_offset = 0 - (len ("END LIST CMD %s\n " % ups ) + 1 )
191191
@@ -195,8 +195,8 @@ def list_commands(self, ups):
195195
196196 # For each var we try to get the available description
197197 try :
198- self ._srv_handler .write ("GET CMDDESC %s %s\n " % (ups , command ))
199- temp = self ._srv_handler .read_until ("\n " , self ._timeout )
198+ self ._srv_handler .write (b "GET CMDDESC %s %s\n " % (ups . encode ( 'utf-8' ) , command . encode ( 'utf-8' ) ))
199+ temp = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
200200 if temp .startswith ("CMDDESC" ):
201201 desc_offset = len ("CMDDESC %s %s " % (ups , command ))
202202 commands [command ] = temp [desc_offset :- 1 ].split ('"' )[1 ]
@@ -219,15 +219,15 @@ def list_clients(self, ups=None):
219219 raise PyNUTError ("%s is not a valid UPS" % ups )
220220
221221 if ups :
222- self ._srv_handler .write ("LIST CLIENTS %s\n " % ups )
222+ self ._srv_handler .write (b "LIST CLIENTS %s\n " % ups . encode ( 'utf-8' ) )
223223 else :
224- self ._srv_handler .write ("LIST CLIENTS\n " )
225- result = self ._srv_handler .read_until ("\n " , self ._timeout )
224+ self ._srv_handler .write (b "LIST CLIENTS\n " )
225+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
226226 if result != "BEGIN LIST CLIENTS\n " :
227227 raise PyNUTError (result .replace ("\n " , "" ))
228228
229- result = self ._srv_handler .read_until ("END LIST CLIENTS\n " ,
230- self ._timeout )
229+ result = self ._srv_handler .read_until (b "END LIST CLIENTS\n " ,
230+ self ._timeout ). decode ( 'utf-8' )
231231
232232 clients = {}
233233 for line in result .split ("\n " ):
@@ -247,13 +247,13 @@ def list_rw_vars(self, ups):
247247 """
248248 logging .debug ("list_vars from '%s'..." , ups )
249249
250- self ._srv_handler .write ("LIST RW %s\n " % ups )
251- result = self ._srv_handler .read_until ("\n " , self ._timeout )
250+ self ._srv_handler .write (b "LIST RW %s\n " % ups . encode ( 'utf-8' ) )
251+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
252252 if result != "BEGIN LIST RW %s\n " % ups :
253253 raise PyNUTError (result .replace ("\n " , "" ))
254254
255- result = self ._srv_handler .read_until ("END LIST RW %s\n " % ups ,
256- self ._timeout )
255+ result = self ._srv_handler .read_until (b "END LIST RW %s\n " % ups . encode ( 'utf-8' ) ,
256+ self ._timeout ). decode ( 'utf-8' )
257257 offset = len ("VAR %s" % ups )
258258 end_offset = 0 - (len ("END LIST RW %s\n " % ups ) + 1 )
259259
@@ -271,13 +271,13 @@ def list_enum(self, ups, var):
271271 """
272272 logging .debug ("list_enum from '%s'..." , ups )
273273
274- self ._srv_handler .write ("LIST ENUM %s %s\n " % (ups , var ))
275- result = self ._srv_handler .read_until ("\n " , self ._timeout )
274+ self ._srv_handler .write (b "LIST ENUM %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ))
275+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
276276 if result != "BEGIN LIST ENUM %s %s\n " % (ups , var ):
277277 raise PyNUTError (result .replace ("\n " , "" ))
278278
279- result = self ._srv_handler .read_until ("END LIST ENUM %s %s\n " % (ups , var ),
280- self ._timeout )
279+ result = self ._srv_handler .read_until (b "END LIST ENUM %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ),
280+ self ._timeout ). decode ( 'utf-8' )
281281 offset = len ("ENUM %s %s" % (ups , var ))
282282 end_offset = 0 - (len ("END LIST ENUM %s %s\n " % (ups , var )) + 1 )
283283
@@ -294,13 +294,13 @@ def list_range(self, ups, var):
294294 """
295295 logging .debug ("list_range from '%s'..." , ups )
296296
297- self ._srv_handler .write ("LIST RANGE %s %s\n " % (ups , var ))
298- result = self ._srv_handler .read_until ("\n " , self ._timeout )
297+ self ._srv_handler .write (b "LIST RANGE %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ))
298+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
299299 if result != "BEGIN LIST RANGE %s %s\n " % (ups , var ):
300300 raise PyNUTError (result .replace ("\n " , "" ))
301301
302- result = self ._srv_handler .read_until ("END LIST RANGE %s %s\n " % (ups , var ),
303- self ._timeout )
302+ result = self ._srv_handler .read_until (b "END LIST RANGE %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ),
303+ self ._timeout ). decode ( 'utf-8' )
304304 offset = len ("RANGE %s %s" % (ups , var ))
305305 end_offset = 0 - (len ("END LIST RANGE %s %s\n " % (ups , var )) + 1 )
306306
@@ -318,17 +318,17 @@ def set_var(self, ups, var, value):
318318 """
319319 logging .debug ("set_var '%s' from '%s' to '%s'" , var , ups , value )
320320
321- self ._srv_handler .write ("SET VAR %s %s %s\n " % (ups , var , value ))
322- result = self ._srv_handler .read_until ("\n " , self ._timeout )
321+ self ._srv_handler .write (b "SET VAR %s %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) , value . encode ( 'utf-8' ) ))
322+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
323323 if result != "OK\n " :
324324 raise PyNUTError (result .replace ("\n " , "" ))
325325
326326 def get_var (self , ups , var ):
327327 """Get the value of a variable."""
328328 logging .debug ("get_var called..." )
329329
330- self ._srv_handler .write ("GET VAR %s %s\n " % (ups , var ))
331- result = self ._srv_handler .read_until ("\n " , self ._timeout )
330+ self ._srv_handler .write (b "GET VAR %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ))
331+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
332332 try :
333333 # result = 'VAR %s %s "%s"\n' % (ups, var, value)
334334 return result .split ('"' )[1 ].strip ()
@@ -344,8 +344,8 @@ def var_description(self, ups, var):
344344 """Get a variable's description."""
345345 logging .debug ("var_description called..." )
346346
347- self ._srv_handler .write ("GET DESC %s %s\n " % (ups , var ))
348- result = self ._srv_handler .read_until ("\n " , self ._timeout )
347+ self ._srv_handler .write (b "GET DESC %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ))
348+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
349349 try :
350350 # result = 'DESC %s %s "%s"\n' % (ups, var, description)
351351 return result .split ('"' )[1 ].strip ()
@@ -356,8 +356,8 @@ def var_type(self, ups, var):
356356 """Get a variable's type."""
357357 logging .debug ("var_type called..." )
358358
359- self ._srv_handler .write ("GET TYPE %s %s\n " % (ups , var ))
360- result = self ._srv_handler .read_until ("\n " , self ._timeout )
359+ self ._srv_handler .write (b "GET TYPE %s %s\n " % (ups . encode ( 'utf-8' ) , var . encode ( 'utf-8' ) ))
360+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
361361 try :
362362 # result = 'TYPE %s %s %s\n' % (ups, var, type)
363363 type_ = ' ' .join (result .split (' ' )[3 :]).strip ()
@@ -372,8 +372,8 @@ def command_description(self, ups, command):
372372 """Get a command's description."""
373373 logging .debug ("command_description called..." )
374374
375- self ._srv_handler .write ("GET CMDDESC %s %s\n " % (ups , command ))
376- result = self ._srv_handler .read_until ("\n " , self ._timeout )
375+ self ._srv_handler .write (b "GET CMDDESC %s %s\n " % (ups . encode ( 'utf-8' ) , command . encode ( 'utf-8' ) ))
376+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
377377 try :
378378 # result = 'CMDDESC %s %s "%s"' % (ups, command, description)
379379 return result .split ('"' )[1 ].strip ()
@@ -384,23 +384,23 @@ def run_command(self, ups, command):
384384 """Send a command to the specified UPS."""
385385 logging .debug ("run_command called..." )
386386
387- self ._srv_handler .write ("INSTCMD %s %s\n " % (ups , command ))
388- result = self ._srv_handler .read_until ("\n " , self ._timeout )
387+ self ._srv_handler .write (b "INSTCMD %s %s\n " % (ups . encode ( 'utf-8' ) , command . encode ( 'utf-8' ) ))
388+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
389389 if result != "OK\n " :
390390 raise PyNUTError (result .replace ("\n " , "" ))
391391
392392 def fsd (self , ups ):
393393 """Send MASTER and FSD commands."""
394394 logging .debug ("MASTER called..." )
395395
396- self ._srv_handler .write ("MASTER %s\n " % ups )
397- result = self ._srv_handler .read_until ("\n " , self ._timeout )
396+ self ._srv_handler .write (b "MASTER %s\n " % ups . encode ( 'utf-8' ) )
397+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
398398 if result != "OK MASTER-GRANTED\n " :
399399 raise PyNUTError (("Master level function are not available" , "" ))
400400
401401 logging .debug ("FSD called..." )
402- self ._srv_handler .write ("FSD %s\n " % ups )
403- result = self ._srv_handler .read_until ("\n " , self ._timeout )
402+ self ._srv_handler .write (b "FSD %s\n " % ups . encode ( 'utf-8' ) )
403+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
404404 if result != "OK FSD-SET\n " :
405405 raise PyNUTError (result .replace ("\n " , "" ))
406406
@@ -410,8 +410,8 @@ def num_logins(self, ups):
410410 """
411411 logging .debug ("num_logins called on '%s'..." , ups )
412412
413- self ._srv_handler .write ("GET NUMLOGINS %s\n " % ups )
414- result = self ._srv_handler .read_until ("\n " , self ._timeout )
413+ self ._srv_handler .write (b "GET NUMLOGINS %s\n " % ups . encode ( 'utf-8' ) )
414+ result = self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
415415 try :
416416 # result = "NUMLOGINS %s %s\n" % (ups, int(numlogins))
417417 return int (result .split (' ' )[2 ].strip ())
@@ -422,12 +422,12 @@ def help(self):
422422 """Send HELP command."""
423423 logging .debug ("HELP called..." )
424424
425- self ._srv_handler .write ("HELP\n " )
426- return self ._srv_handler .read_until ("\n " , self ._timeout )
425+ self ._srv_handler .write (b "HELP\n " )
426+ return self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
427427
428428 def ver (self ):
429429 """Send VER command."""
430430 logging .debug ("VER called..." )
431431
432- self ._srv_handler .write ("VER\n " )
433- return self ._srv_handler .read_until ("\n " , self ._timeout )
432+ self ._srv_handler .write (b "VER\n " )
433+ return self ._srv_handler .read_until (b "\n " , self ._timeout ). decode ( 'utf-8' )
0 commit comments