@@ -47,13 +47,13 @@ class PyNUTClient :
4747 __login = None
4848 __password = None
4949 __timeout = None
50- __srv_handler = None
50+ _srv_handler = None
5151
5252 __version = "1.2.2"
5353 __release = "2012-02-07"
5454
5555
56- def __init__ ( self , host = "127.0.0.1" , port = 3493 , login = None , password = None , debug = False , timeout = 5 ) :
56+ def __init__ ( self , host = "127.0.0.1" , port = 3493 , login = None , password = None , debug = False , timeout = 5 , connect = True ) :
5757 """ Class initialization method
5858
5959host : Host to connect (default to localhost)
@@ -76,13 +76,14 @@ def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debu
7676 self .__password = password
7777 self .__timeout = 5
7878
79- self .__connect ()
79+ if connect :
80+ self .__connect ()
8081
8182 # Try to disconnect cleanly when class is deleted ;)
8283 def __del__ ( self ) :
8384 """ Class destructor method """
8485 try :
85- self .__srv_handler .write ( "LOGOUT\n " )
86+ self ._srv_handler .write ( "LOGOUT\n " )
8687 except :
8788 pass
8889
@@ -95,17 +96,17 @@ def __connect( self ) :
9596 if self .__debug :
9697 print ( "[DEBUG] Connecting to host" )
9798
98- self .__srv_handler = telnetlib .Telnet ( self .__host , self .__port )
99+ self ._srv_handler = telnetlib .Telnet ( self .__host , self .__port )
99100
100101 if self .__login != None :
101- self .__srv_handler .write ( "USERNAME %s\n " % self .__login )
102- result = self .__srv_handler .read_until ( "\n " , self .__timeout )
102+ self ._srv_handler .write ( "USERNAME %s\n " % self .__login )
103+ result = self ._srv_handler .read_until ( "\n " , self .__timeout )
103104 if result [:2 ] != "OK" :
104105 raise Exception , result .replace ( "\n " , "" )
105106
106107 if self .__password != None :
107- self .__srv_handler .write ( "PASSWORD %s\n " % self .__password )
108- result = self .__srv_handler .read_until ( "\n " , self .__timeout )
108+ self ._srv_handler .write ( "PASSWORD %s\n " % self .__password )
109+ result = self ._srv_handler .read_until ( "\n " , self .__timeout )
109110 if result [:2 ] != "OK" :
110111 raise Exception , result .replace ( "\n " , "" )
111112
@@ -117,12 +118,12 @@ def GetUPSList( self ) :
117118 if self .__debug :
118119 print ( "[DEBUG] GetUPSList from server" )
119120
120- self .__srv_handler .write ( "LIST UPS\n " )
121- result = self .__srv_handler .read_until ( "\n " )
121+ self ._srv_handler .write ( "LIST UPS\n " )
122+ result = self ._srv_handler .read_until ( "\n " )
122123 if result != "BEGIN LIST UPS\n " :
123124 raise Exception , result .replace ( "\n " , "" )
124125
125- result = self .__srv_handler .read_until ( "END LIST UPS\n " )
126+ result = self ._srv_handler .read_until ( "END LIST UPS\n " )
126127 ups_list = {}
127128
128129 for line in result .split ( "\n " ) :
@@ -141,13 +142,13 @@ def GetUPSVars( self, ups="" ) :
141142 if self .__debug :
142143 print ( "[DEBUG] GetUPSVars called..." )
143144
144- self .__srv_handler .write ( "LIST VAR %s\n " % ups )
145- result = self .__srv_handler .read_until ( "\n " )
145+ self ._srv_handler .write ( "LIST VAR %s\n " % ups )
146+ result = self ._srv_handler .read_until ( "\n " )
146147 if result != "BEGIN LIST VAR %s\n " % ups :
147148 raise Exception , result .replace ( "\n " , "" )
148149
149150 ups_vars = {}
150- result = self .__srv_handler .read_until ( "END LIST VAR %s\n " % ups )
151+ result = self ._srv_handler .read_until ( "END LIST VAR %s\n " % ups )
151152 offset = len ( "VAR %s " % ups )
152153 end_offset = 0 - ( len ( "END LIST VAR %s\n " % ups ) + 1 )
153154
@@ -167,13 +168,13 @@ def GetUPSCommands( self, ups="" ) :
167168 if self .__debug :
168169 print ( "[DEBUG] GetUPSCommands called..." )
169170
170- self .__srv_handler .write ( "LIST CMD %s\n " % ups )
171- result = self .__srv_handler .read_until ( "\n " )
171+ self ._srv_handler .write ( "LIST CMD %s\n " % ups )
172+ result = self ._srv_handler .read_until ( "\n " )
172173 if result != "BEGIN LIST CMD %s\n " % ups :
173174 raise Exception , result .replace ( "\n " , "" )
174175
175176 ups_cmds = {}
176- result = self .__srv_handler .read_until ( "END LIST CMD %s\n " % ups )
177+ result = self ._srv_handler .read_until ( "END LIST CMD %s\n " % ups )
177178 offset = len ( "CMD %s " % ups )
178179 end_offset = 0 - ( len ( "END LIST CMD %s\n " % ups ) + 1 )
179180
@@ -182,8 +183,8 @@ def GetUPSCommands( self, ups="" ) :
182183
183184 # For each var we try to get the available description
184185 try :
185- self .__srv_handler .write ( "GET CMDDESC %s %s\n " % ( ups , var ) )
186- temp = self .__srv_handler .read_until ( "\n " )
186+ self ._srv_handler .write ( "GET CMDDESC %s %s\n " % ( ups , var ) )
187+ temp = self ._srv_handler .read_until ( "\n " )
187188 if temp [:7 ] != "CMDDESC" :
188189 raise
189190 else :
@@ -204,12 +205,12 @@ def GetRWVars( self, ups="" ) :
204205 if self .__debug :
205206 print ( "[DEBUG] GetUPSVars from '%s'..." % ups )
206207
207- self .__srv_handler .write ( "LIST RW %s\n " % ups )
208- result = self .__srv_handler .read_until ( "\n " )
208+ self ._srv_handler .write ( "LIST RW %s\n " % ups )
209+ result = self ._srv_handler .read_until ( "\n " )
209210 if ( result != "BEGIN LIST RW %s\n " % ups ) :
210211 raise Exception , result .replace ( "\n " , "" )
211212
212- result = self .__srv_handler .read_until ( "END LIST RW %s\n " % ups )
213+ result = self ._srv_handler .read_until ( "END LIST RW %s\n " % ups )
213214 offset = len ( "VAR %s" % ups )
214215 end_offset = 0 - ( len ( "END LIST RW %s\n " % ups ) + 1 )
215216 rw_vars = {}
@@ -232,8 +233,8 @@ def SetRWVar( self, ups="", var="", value="" ):
232233rights to set it (maybe login/password).
233234 """
234235
235- self .__srv_handler .write ( "SET VAR %s %s %s\n " % ( ups , var , value ) )
236- result = self .__srv_handler .read_until ( "\n " )
236+ self ._srv_handler .write ( "SET VAR %s %s %s\n " % ( ups , var , value ) )
237+ result = self ._srv_handler .read_until ( "\n " )
237238 if ( result == "OK\n " ) :
238239 return ( "OK" )
239240 else :
@@ -248,8 +249,8 @@ def RunUPSCommand( self, ups="", command="" ) :
248249 if self .__debug :
249250 print ( "[DEBUG] RunUPSCommand called..." )
250251
251- self .__srv_handler .write ( "INSTCMD %s %s\n " % ( ups , command ) )
252- result = self .__srv_handler .read_until ( "\n " )
252+ self ._srv_handler .write ( "INSTCMD %s %s\n " % ( ups , command ) )
253+ result = self ._srv_handler .read_until ( "\n " )
253254 if ( result == "OK\n " ) :
254255 return ( "OK" )
255256 else :
@@ -264,15 +265,15 @@ def FSD( self, ups="") :
264265 if self .__debug :
265266 print ( "[DEBUG] MASTER called..." )
266267
267- self .__srv_handler .write ( "MASTER %s\n " % ups )
268- result = self .__srv_handler .read_until ( "\n " )
268+ self ._srv_handler .write ( "MASTER %s\n " % ups )
269+ result = self ._srv_handler .read_until ( "\n " )
269270 if ( result != "OK MASTER-GRANTED\n " ) :
270271 raise Exception , ( "Master level function are not available" , "" )
271272
272273 if self .__debug :
273274 print ( "[DEBUG] FSD called..." )
274- self .__srv_handler .write ( "FSD %s\n " % ups )
275- result = self .__srv_handler .read_until ( "\n " )
275+ self ._srv_handler .write ( "FSD %s\n " % ups )
276+ result = self ._srv_handler .read_until ( "\n " )
276277 if ( result == "OK FSD-SET\n " ) :
277278 return ( "OK" )
278279 else :
@@ -285,8 +286,8 @@ def help(self) :
285286 if self .__debug :
286287 print ( "[DEBUG] HELP called..." )
287288
288- self .__srv_handler .write ( "HELP\n " )
289- return self .__srv_handler .read_until ( "\n " )
289+ self ._srv_handler .write ( "HELP\n " )
290+ return self ._srv_handler .read_until ( "\n " )
290291
291292 def ver (self ) :
292293 """ Send VER command
@@ -295,8 +296,8 @@ def ver(self) :
295296 if self .__debug :
296297 print ( "[DEBUG] VER called..." )
297298
298- self .__srv_handler .write ( "VER\n " )
299- return self .__srv_handler .read_until ( "\n " )
299+ self ._srv_handler .write ( "VER\n " )
300+ return self ._srv_handler .read_until ( "\n " )
300301
301302 def ListClients ( self , ups = None ) :
302303 """ Returns the list of connected clients from the NUT server
@@ -310,14 +311,16 @@ def ListClients( self, ups = None ) :
310311 raise Exception , "%s is not a valid UPS" % ups
311312
312313 if ups :
313- self .__srv_handler .write ( "LIST CLIENTS %s\n " % ups )
314+ self ._srv_handler .write ( "LIST CLIENTS %s\n " % ups )
314315 else :
315- self .__srv_handler .write ( "LIST CLIENTS\n " )
316- result = self .__srv_handler .read_until ( "\n " )
316+ self ._srv_handler .write ( "LIST CLIENTS\n " )
317+ result = self ._srv_handler .read_until ( "\n " )
317318 if result != "BEGIN LIST CLIENTS\n " :
318319 raise Exception , result .replace ( "\n " , "" )
319320
320- result = self .__srv_handler .read_until ( "END LIST CLIENTS\n " )
321+ result = self ._srv_handler .read_until ( "END LIST CLIENTS\n " )
322+ ups_list = {}
323+
321324 for line in result .split ( "\n " ):
322325 if line [:6 ] == "CLIENT" :
323326 host , ups = line [7 :].split (' ' )
0 commit comments